Introduction to basic knowledge:
Process Class
Provides access to local and remote processes and enables you to start and stop Local system processes
Namespaces: System.Diagnostics
Assembly: System (in System.dll)
Syntax: public class Process:component
Process.Start Method (ProcessStartInfo)
Starts the process resource specified by the parameter that contains the process startup information (for example, the file name of the process to start) and associates the resource with the new Process component syntax: public static process Start (ProcessStartInfo startinfo)
The process component provides access to the processes that are running on the computer. In the shortest words, the process is the currently running application. A thread is the basic unit to which the operating system allocates processor time. The thread can execute any part of the code of the process, including the part that is currently being executed by another.
The Process component is a useful tool for tasks such as starting, stopping, controlling, and monitoring applications. Using the process component, you can get a list of the processes that are running, or you can start a new process. The process component is used to access system processes. After you initialize the process component, you can use the component to obtain information about the currently running process. This information includes line threads, loaded modules (. dll and. exe files), and performance information, such as the amount of memory currently used by the process.
If you declare a path variable in quotation marks in the system, you must fully qualify the path when you start any process in that location. Otherwise, the system will not find the path. For example, if C:\mypath is not in your path, and you use quotation marks to add it (path =%path%; " C:\mypath "), you must fully qualify them when you start any process in C:\mypath.
The process component obtains information about a set of properties at the same time. When the Process component obtains information about one member of any group, it caches the values of other properties in that group, and does not get new information about other members of the group until you call the Refresh method. Therefore, the property value is not guaranteed to be newer than the last call to the Refresh method. Group segmentation is related to the operating system.
System processes are uniquely identified by their process identifiers on the system. As with many Windows resources, a process is identified by its handle, and the handle may not be unique on the computer. A handle is a generic term that represents a resource identifier. The operating system maintains a process handle even though the process has exited, and the handle is accessed through the Handle property of the process component. Therefore, you can obtain management information for the process, such as ExitCode (usually, or zero for success, or for a non-0 error code) and Exittime. Handles are very valuable resources, so handle leaks are more harmful than memory leaks.
ProcessStartInfo class
Specifies a set of values to use when starting a process.
Syntax: public sealed class ProcessStartInfo
Processstartinfo.filename Property
Gets or sets the application or document to launch
Syntax: public string filename{get; set;}
ProcessStartInfo is used in conjunction with the Process component. When you start a process using the process class, you also have access to process information other than the process information that is available when you attach to the currently running process.
You can use the ProcessStartInfo class to better control which processes are started. You must set the FileName property at least manually or by using a constructor. The file name is any application or document. Here, the document is defined as any file type that has an open or default action associated with it. Use the Folder Options dialog box provided by the operating system to view the registered file types and their associated applications on your computer. Click the Advanced button to open a dialog box that shows whether there is an open action associated with a specific registered file type.
In addition, you can set additional properties that define the actions you want to perform on the file. You can specify the value of the type that is specific to the FileName property for the Verb property. For example, you can specify "print" for the document type. Alternatively, you can specify the Arguments property value, which will be the command-line argument for the open procedure passed to the file. For example, if you specify a text editor application in the FileName property, you can use the Arguments property to specify a text file that will be opened with the editor.
Standard input methods are typically keyboards, and standard output and standard errors are usually displayed on the monitor screen. However, you can use the Redirectstandardinput, Redirectstandardoutput, and Redirectstandarderror properties to make the process get input from a file or other device, or to return the output to a file or other device. If you use the Standardinput, StandardOutput, or StandardError properties on the Process component, you must first set the corresponding value on the ProcessStartInfo property. Otherwise, the system throws an exception when reading from a stream or writing to a stream.
Set UseShellExecute to specify whether to use the operating system shell to start the process.
You can change the value of any ProcessStartInfo property until the process starts. After the process is started, changing these values has no effect.
Practical application:
Create a new WPF application and add a button to the. xaml file
To add a click event to a button
Add the following code to the click function of the. CS:
private void button1_Click(object sender, RoutedEventArgs e){ System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo(); Info.FileName"calc.exe ";//"calc.exe"为计算器,"notepad.exe"为记事本System.Diagnostics.Process Proc = System.Diagnostics.Process.Start(Info);}
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C # calls Microsoft's own calculator, Notepad, and other software