Hey - What does this button do?
Hi, Eric here again. A question came in to us, via email, to my post about "shelling" in .NET. I thought I would respond with a new blog entry to share with the community.
The question was:
From: Andreas
Sent: Friday, July 22, 2005 7:13 AM
Hi Eric
I liked your piece of code that sends keystorkes too call wordpad.
I am writing a mobile application and I
was trying to output information from
the application to a printer.
I have written the information onto a
text file in the windows CE mobile, since
I could not find a way to output the text
to the printer.
I have seen your code I thought, if I
could get wordpad to read the text
file and output to the printer, by sending
keystrokes.
Please let me know if you have any ideas.
Many Thanks
Andreas
Thank you for the question, Andreas. To perform what you describe, I wrote some sample code for you, included below. It:
1. opens wordpad
2. opens a file
3. prints it
4. closes wordpad
And you can give it a command line argument to specify the location of a file to print:
shell.exe C:\WINDOWS\system32\clusoc.txt
If you don't give it this input argument, it has a hardcoded file path it tries to open.
Good luck!
- Eric
_______
using System;using System.Text;using System.Diagnostics;using System.Threading;using System.Windows.Forms;namespace shell{class shell
{
static void Main(string[] args)
{
//"shell" to open WordPad
Process myProcess = new Process();
myProcess.StartInfo.FileName = @"wordpad.exe";
myProcess.StartInfo.Verb = "Open";
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
myProcess.Start();
//pause for 0.5 seconds to give the application time to open
Thread.Sleep(500);
//look for input argument that indicates a file path
string PrintThisFile = string.Empty;
if (args.Length > 0)
PrintThisFile = args[0];
else
PrintThisFile = @"C:\WINDOWS\system32\perffilt.h";
//open a file
SendKeys.SendWait("%FO");
SendKeys.SendWait(PrintThisFile + "~");
//print
Thread.Sleep(500);
SendKeys.SendWait("%FP");
Thread.Sleep(500);
SendKeys.SendWait("~");
//close the active window
Thread.Sleep(3000);
SendKeys.SendWait("+(%{F4})");
}
}
}
New Comments to this post are disabled