c# 执行 cmd 命令并获取返回结果

一次性返回结果:

public static string ExecuteCommandSync(object command)
      {
          try
          {
              // create the ProcessStartInfo using "cmd" as the program to be run,
              // and "/c " as the parameters.
              // Incidentally, /c tells cmd that we want it to execute the command that follows,
              // and then exit.
              System.Diagnostics.ProcessStartInfo procStartInfo =
                  new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

              // The following commands are needed to redirect the standard output.
              // This means that it will be redirected to the Process.StandardOutput StreamReader.
              procStartInfo.RedirectStandardOutput = true;
              procStartInfo.UseShellExecute = false;
              // Do not create the black window.
              procStartInfo.CreateNoWindow = true;
              // Now we create a process, assign its ProcessStartInfo and start it
              System.Diagnostics.Process proc = new System.Diagnostics.Process();
              proc.StartInfo = procStartInfo;
              proc.Start();
              // Get the output into a string
              string result = proc.StandardOutput.ReadToEnd();
              // Display the command output.
              return result;
          }
          catch (Exception)
          {
              // Log the exception
              return null;
          }
      }

使用:

string cm1 = "wmic diskdrive get SerialNumber | more +1";
ExecuteCommandSync(cm1);

将结果按行返回:

这里将返回结果按行存放到 list 里;

public static List<string> ExecuteCommand(object command)
        {
            try
            {
                List<string> list = new List<string>();
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo =
                    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;

                proc.Start();
                // Get the output into a string
                //string result = proc.StandardOutput.ReadToEnd();
                StreamReader sr = proc.StandardOutput;//获取返回值 
                string line = "";
                int num = 1;
                while ((line = sr.ReadLine()) != null) //按行读取
                {
                    if (line != "")
                    {
                        //Console.WriteLine(line + " " + num++);
                        list.Add(line.Trim());
                    }
                }
                //return result;
                return list;
            }
            catch (Exception)
            {
                // Log the exception
                return null;
            }
        }

使用:

string cm1 = "wmic diskdrive get SerialNumber | more +1";
ExecuteCommand(cm1);

参考:

https://stackoverflow.com/questions/4084402/get-hard-disk-serial-number

https://blog.csdn.net/sinat_25185707/article/details/82467588