透過 C# 執行自製的 exe 及 cmd 指令

  • 11306
  • 0
  • C#
  • 2017-05-31

透過 C# 執行自製的 exe 及 cmd 指令 & 傳遞參數的方法

System.Diagnostics.Process exe = new System.Diagnostics.Process();
exe.StartInfo.FileName = EXEPath + @"\EXENAME.EXE";
exe.StartInfo.Arguments = DATA;
exe.Start();
exe.WaitForExit();
exe.Close();

或者透過CMD執行

Process process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.Start();

process.StandardInput.WriteLine(@"cd EXEPath " + data + "& exit");
process.WaitForExit();
process.Close();

如果需要等待延遲的話

int millisecondsTimeout = 2000;
System.Threading.Thread.Sleep(millisecondsTimeout);

刪除未完成的EXE
 

var DriverProcesses = Process.GetProcesses().
                 Where(pr => pr.ProcessName == "XXX");
foreach (var process in DriverProcesses)
{
    process.Kill();
}

 

 

以上內容,若有錯誤

煩請各路高手路過指正

謝謝!

<(_ _)>