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: AndreasSent: Friday, July 22, 2005 7:13 AMHi Eric I liked your piece of code that sends keystorkes too call wordpad.I am writing a mobile application and Iwas trying to output information from the application to a printer.I have written the information onto a text file in the windows CE mobile, sinceI could not find a way to output the textto the printer.I have seen your code I thought, if Icould get wordpad to read the textfile and output to the printer, by sendingkeystrokes.Please let me know if you have any ideas. Many ThanksAndreas
1. opens wordpad2. opens a file3. prints it4. 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
_______
{ static void Main(string[] args){ //"shell" to open WordPadProcess 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 openThread.Sleep(500);//look for input argument that indicates a file pathstring PrintThisFile = string.Empty;if (args.Length > 0) PrintThisFile = args[0];else PrintThisFile = @"C:\WINDOWS\system32\perffilt.h";//open a fileSendKeys.SendWait("%FO");SendKeys.SendWait(PrintThisFile + "~");//printThread.Sleep(500);SendKeys.SendWait("%FP");Thread.Sleep(500);SendKeys.SendWait("~");//close the active windowThread.Sleep(3000);SendKeys.SendWait("+(%{F4})"); } }
{
static void Main(string[] args){ //"shell" to open WordPadProcess 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 openThread.Sleep(500);//look for input argument that indicates a file pathstring PrintThisFile = string.Empty;if (args.Length > 0) PrintThisFile = args[0];else PrintThisFile = @"C:\WINDOWS\system32\perffilt.h";//open a fileSendKeys.SendWait("%FO");SendKeys.SendWait(PrintThisFile + "~");//printThread.Sleep(500);SendKeys.SendWait("%FP");Thread.Sleep(500);SendKeys.SendWait("~");//close the active windowThread.Sleep(3000);SendKeys.SendWait("+(%{F4})"); }
//"shell" to open WordPadProcess 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 openThread.Sleep(500);//look for input argument that indicates a file pathstring PrintThisFile = string.Empty;if (args.Length > 0) PrintThisFile = args[0];else PrintThisFile = @"C:\WINDOWS\system32\perffilt.h";//open a fileSendKeys.SendWait("%FO");SendKeys.SendWait(PrintThisFile + "~");//printThread.Sleep(500);SendKeys.SendWait("%FP");Thread.Sleep(500);SendKeys.SendWait("~");//close the active windowThread.Sleep(3000);SendKeys.SendWait("+(%{F4})");
PrintThisFile = args[0];
PrintThisFile = @"C:\WINDOWS\system32\perffilt.h";
}