Sunday, May 20, 2007

Starting new process

In the last post, I explained how to get a list of existing process on local or remote machine.
Now I want to explain how to start a new process in C#.

If you create an object of Process class, you can set some information on StartInfo property of the object to specify what to do when you start the process. In the line below I 'm going to Print a word document in my C# sample:

Process printProcess = new Process();
OpenFileDialog op = new OpenFileDialog();
op.Filter = "Microsoft Word Document (*.doc)*.doc";
if(op.ShowDialog() == DialogResult.OK)

printProcess.StartInfo.FileName = op.FileName;
printProcess.StartInfo.Verb = "Print";
printProcess.StartInfo.CreateNoWindow = true;
catch(Win32Exception ex)

if(e.NativeErrorCode ==2)
MessageBox.Show(e.Message + ". Check the path.");
else if(e.NativeErrorCode == 5)
MessageBox.Show(e.Message + ". You do not have permission to print this file.");

Notice that I 've used "Print" for Verb property of StartInfo. If you don't know what are available verbs on a extension (if it 's not executable) you can get list of verbs by using Verbs proerty of the process. Just like this:

ProcessStartInfo stInfo = new ProcessStartInfo(fileNameWithExtension);
foreach(string verb in stInfo.Verbs)

Console.WriteLine(" {0}",verb);

Notice that after you ran the process, changing the value of StartInfo property does not effect on the running process.

And you can use specific username and password withing UserName,Password property in StartInfo but if you set thses property the process starts in new window even if the CreateNoWindow property value is true of the WindowStyle property value is Hidden.

No comments: