1. 程式人生 > >C# 執行CMD命令

C# 執行CMD命令

  private static string ExcuteWinCmdStr(string cmdStr)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.WriteLine(cmdStr);
            p.StandardInput.WriteLine("exit");
             return p.StandardOutput.ReadToEnd();           
        }
private static string ExcuteWinCmdStr(string cmdStr)
{
    Process process = new Process {
        StartInfo = { FileName = "cmd.exe", UseShellExecute = false, RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }
    };
    process.Start();
    process.StandardInput.WriteLine(cmdStr);
    process.StandardInput.WriteLine("exit");
    return process.StandardOutput.ReadToEnd();
}