1. 程式人生 > 其它 >c# 返回 cmd 命令的結果

c# 返回 cmd 命令的結果

把結果作為一個整體返回:

 1 public static string ExecuteCommandSync(object command)
 2       {
 3           try
 4           {
 5               // create the ProcessStartInfo using "cmd" as the program to be run,
 6               // and "/c " as the parameters.
 7               // Incidentally, /c tells cmd that we want it to execute the command that follows,
8 // and then exit. 9 System.Diagnostics.ProcessStartInfo procStartInfo = 10 new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command); 11 12 // The following commands are needed to redirect the standard output. 13 // This means that it will be redirected to the Process.StandardOutput StreamReader.
14 procStartInfo.RedirectStandardOutput = true; 15 procStartInfo.UseShellExecute = false; 16 // Do not create the black window. 17 procStartInfo.CreateNoWindow = true; 18 // Now we create a process, assign its ProcessStartInfo and start it
19 System.Diagnostics.Process proc = new System.Diagnostics.Process(); 20 proc.StartInfo = procStartInfo; 21 proc.Start(); 22 // Get the output into a string 23 string result = proc.StandardOutput.ReadToEnd(); 24 // Display the command output. 25 return result; 26 } 27 catch (Exception) 28 { 29 // Log the exception 30 return null; 31 } 32 }

將結果按行返回:

 1 public static List<string> ExecuteCommand(object command)
 2         {
 3             try
 4             {
 5                 List<string> list = new List<string>();
 6                 // create the ProcessStartInfo using "cmd" as the program to be run,
 7                 // and "/c " as the parameters.
 8                 // Incidentally, /c tells cmd that we want it to execute the command that follows,
 9                 // and then exit.
10                 System.Diagnostics.ProcessStartInfo procStartInfo =
11                     new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
12 
13                 // The following commands are needed to redirect the standard output.
14                 // This means that it will be redirected to the Process.StandardOutput StreamReader.
15                 procStartInfo.RedirectStandardOutput = true;
16                 procStartInfo.UseShellExecute = false;
17                 // Do not create the black window.
18                 procStartInfo.CreateNoWindow = true;
19                 // Now we create a process, assign its ProcessStartInfo and start it
20                 System.Diagnostics.Process proc = new System.Diagnostics.Process();
21                 proc.StartInfo = procStartInfo;
22 
23                 proc.Start();
24                 // Get the output into a string
25                 //string result = proc.StandardOutput.ReadToEnd();
26                 StreamReader sr = proc.StandardOutput;//獲取返回值 
27                 string line = "";
28                 int num = 1;
29                 while ((line = sr.ReadLine()) != null) //按行讀取
30                 {
31                     if (line != "")
32                     {
33                         //Console.WriteLine(line + " " + num++);
34                         list.Add(line.Trim());
35                     }
36                 }
37                 //return result;
38                 return list;
39             }
40             catch (Exception)
41             {
42                 // Log the exception
43                 return null;
44             }
45         }

參考:

C# 執行 CMD 命令並獲取返回結果