2010-11-08 82 views
3

如何从c#运行CMD而无需查看cmd窗口?如何从c#运行CMD与隐藏的CMD

+0

退房参数http://stackoverflow.com/questions/1096591/how-to-hide-cmd-window-while-running-a-batch-file - 与你想要做的非常相似 – InSane 2010-11-08 08:50:34

回答

7

ProcessStartInfo有一个名为CreateNoWindow

public static string ExecuteCommand(string command) { 

    ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command) 
     { 
      RedirectStandardError = true, 
      RedirectStandardOutput = true, 
      UseShellExecute = false, 
      CreateNoWindow = true 
     }; 

    using (Process proc = new Process()) 
    { 
     proc.StartInfo = procStartInfo; 
     proc.Start(); 

     string output = proc.StandardOutput.ReadToEnd(); 

     if (string.IsNullOrEmpty(output)) 
      output = proc.StandardError.ReadToEnd(); 

     return output; 
    } 

}