Sometimes you need to run other programs in the program and obtain output results for further processing .. NET Framework has provided corresponding classes and methods, and we can easily achieve our goal.
The following example is used previously. It mainly uses the Process class and ProcessStartInfo class.
The main function of the Process class is to provide access to local and remote processes and enable you to start and stop local system processes.
The ProcessStartInfo class mainly serves to specify a group of values used to start a Process for the Process class.
Code 1 Process process = new Process ();
2 ProcessStartInfo startInfo = new ProcessStartInfo ("cmd.exe ");
3 startInfo. UseShellExecute = false;
4 process. StartInfo = startInfo;
5 process. StartInfo. RedirectStandardInput = true;
6 process. StartInfo. RedirectStandardOutput = true;
7 process. Start ();
8
9 process. StandardInput. WriteLine ("netstat-");
10 process. StandardInput. WriteLine ("exit ");
11
12 string netMessage = process. StandardOutput. ReadToEnd ();
13 process. WaitForExit ();
14 process. Close ();
15 return netMessage;
The above code is used to open a console in a new process, run the "netstat-an" command, and obtain the output result.