public sealed class ProcessStartInfo
|
You can use the ProcessStartInfo class for greater control over the process you start. You must at least set the ProcessStartInfo.FileName property, either manually or using the constructor. The file name is any application or document. Here a document is defined to be any file type that has an open or default action associated with it. You can view registered file types and their associated applications for your computer using the Folder Options dialog available through the operating system. The Advanced button leads to a dialog that shows whether there is an open action associated with a specific registered file type.
In addition, you can set other properties that define actions to take with that file. You can specify a value specific to the type of the ProcessStartInfo.FileName property for the System.Diagnostics.ProcessStartInfo.Verb (not supported on the shared source CLI) property. For example, you can specify "print" for a document type. Additionally, you can specify ProcessStartInfo.Arguments property values to be command line arguments to pass to the file's open procedure. For example, if you specify a text editor application in the ProcessStartInfo.FileName property, you can use the ProcessStartInfo.Arguments property to specify a text file to be opened by the editor.
Standard input is usually the keyboard, and standard output and error are usually the monitor screen. However, you can use the ProcessStartInfo.RedirectStandardInput, ProcessStartInfo.RedirectStandardOutput and ProcessStartInfo.RedirectStandardError properties to cause the process to get input from or return output to a file or other device. If you use the Process.StandardInput, Process.StandardOutput, or Process.StandardError properties on the Process component, you must first set the corresponding value on the ProcessStartInfo property. Otherwise, the system throws an exception when you read or write to the stream.
Set ProcessStartInfo.UseShellExecute to specify whether to start the process using the operating system shell.
You can change the value of any ProcessStartInfo property up to the time that the process starts. After you start the process, changing these values has no effect.
ctor #1 | Overloaded:.ctor() Default constructor. This constructor is called by derived class constructors to initialize state in this type.Initializes a new instance of the ProcessStartInfo class without specifying a file name with which to start the process. |
ctor #2 | Overloaded:.ctor(string fileName) Initializes a new instance of the ProcessStartInfo class and specifies a file name such as an application or document with which to start the process. |
ctor #3 | Overloaded:.ctor(string fileName, string arguments) Initializes a new instance of the ProcessStartInfo class and specifies an application file name with which to start the process, as well as a set of command line arguments to pass to the application. |
Arguments | Read-write Gest or sets the set of command line arguments to use when starting the application. |
EnvironmentVariables | Read-only Gets search paths for files, directories for temporary files, application-specific options, and other similar information. |
FileName | Read-write |
RedirectStandardError | Read-write Gets or sets a value indicating whether the process's error output is written to the Process instance's Process.StandardError member, enabling you to write to a destination other than the standard error stream (usually the monitor screen). Used to write error data to a file or a log, for example. |
RedirectStandardInput | Read-write Gets or sets a value indicating whether the process command input is read from the Process instance's Process.StandardInput member, enabling you to read from a source other than the standard input stream (usually the keyboard). Used to read data from a file, for example. |
RedirectStandardOutput | Read-write Gets or sets a value indicating whether the process output is written to the Process instance's Process.StandardOutput member, enabling you to write to a destination other than the standard output stream (usually the monitor screen). Used to write data to a file, for example. |
UseShellExecute | Read-write Gets or sets a value indicating whether to use the operating system shell to start the process. |
WorkingDirectory | Read-write Gets or sets the initial directory for the process to be started. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize (inherited from System.Object) |
See base class member description: System.Object.Finalize Derived from System.Object, the primary base class for all objects. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public ProcessStartInfo(); |
Optionally, you can also set other properties before starting the process. The System.Diagnostics.ProcessStartInfo.Verb (not supported on the shared source CLI) property supplies actions to take, such as "print", with the file indicated in the ProcessStartInfo.FileName property. The ProcessStartInfo.Arguments property supplies a way to pass command line arguments to the file when the system opens it.
public ProcessStartInfo( |
fileName
You can change the ProcessStartInfo.FileName property after calling this constructor, up to the time that the process starts. After you start the process, changing these values has no effect.
fileName
arguments
You can change the ProcessStartInfo.FileName or ProcessStartInfo.Arguments properties after calling this constructor, up to the time that the process starts. After you start the process, changing these values has no effect.
public string Arguments {get; set;}
|
public StringDictionary EnvironmentVariables {get;}
|
public string FileName {get; set;}
|
public bool RedirectStandardError {get; set;}
|
Process p = new Process("...", "..."); p.UseShellExecute = false; p.RedirectStandardOutput = true; p.Start(); p.WaitForExit(); string output = p.StandardOutput.ReadToEnd();
In this instance, both the parent and the child processes would be blocked, as the filled pipe prevents the child process from completing, while the parent process is waiting indefinitely for the child process to exit.
This problem can be solved by moving the
ReadToEnd()
before the
WaitForExit()
, as follows.
Process p = new Process("...", "..."); p.UseShellExecute = false; p.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();
A similar problem arises if you redirect both standard output and standard error and then try to read both, for example using the following C# code.
string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit();
In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.
public bool RedirectStandardInput {get; set;}
|
public bool RedirectStandardOutput {get; set;}
|
Process p = new Process("...", "..."); p.UseShellExecute = false; p.RedirectStandardOutput = true; p.Start(); p.WaitForExit(); string output = p.StandardOutput.ReadToEnd();
In this instance, both the parent and the child processes would be blocked, as the filled pipe prevents the child process from completing, while the parent process is waiting indefinitely for the child process to exit.
This problem can be solved by moving the
ReadToEnd()
before the
WaitForExit()
, as follows.
Process p = new Process("...", "..."); p.UseShellExecute = false; p.RedirectStandardOutput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); p.WaitForExit();
A similar problem arises if you redirect both standard output and standard error and then try to read both, for example using the following C# code.
string output = p.StandardOutput.ReadToEnd(); string error = p.StandardError.ReadToEnd(); p.WaitForExit();
In this case, if the child process writes any text to standard error it will block the process, because the parent process cannot read from standard error until it has finished reading from standard output. However, the parent process will not read from standard output until the process ends. A recommended solution to this situation is to create two threads so that your application can read the output of each stream on a separate thread.
public bool UseShellExecute {get; set;}
|
When you use the operating system shell to start processes, you are able to start any document (which is any registered file type associated with an executable that has a default open action) and perform operations on the file, such as printing, with the Process component. When ProcessStartInfo.UseShellExecute is false, you are able to start only executables with the Process component.
public string WorkingDirectory {get; set;}
|
~ProcessStartInfo(); |
public virtual int GetHashCode(); |
public Type GetType(); |
protected object MemberwiseClone(); |
public virtual string ToString(); |