Execute CMD command from code

后端 未结 10 1153
故里飘歌
故里飘歌 2020-11-30 06:38

In C# WPF: I want to execute a CMD command, how exactly can I execute a cmd command programmatically?

10条回答
  •  一生所求
    2020-11-30 06:54

    You can do like below:

    var command = "Put your command here";
    System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;
    procStartInfo.WorkingDirectory = @"C:\Program Files\IIS\Microsoft Web Deploy V3";
    procStartInfo.CreateNoWindow = true; //whether you want to display the command window
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();
    string result = proc.StandardOutput.ReadToEnd();
    label1.Text = result.ToString();
    

提交回复
热议问题