C# Process的使用


private static string ExecuteCmd(string wrokDirectory, string dosCommand)
{
string output = string.Empty;

if (string.IsNullOrEmpty(wrokDirectory) || string.IsNullOrEmpty(dosCommand))
{
return output;
}

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
//startInfo.Arguments = "/C " + dosCommand; //設定參數,其中的“/C”表示執行完命令后馬上退出
startInfo.UseShellExecute = false; //不使用系統外殼程序啟動
startInfo.RedirectStandardInput = false; //不重定向輸入
startInfo.RedirectStandardOutput = true; //重定向輸出
startInfo.CreateNoWindow = true; //不創建窗口
//startInfo.WorkingDirectory = wrokDirectory;
process.StartInfo = startInfo;

try
{
if (process.Start()) //開始進程
{
process.StandardOutput.ReadToEnd(); //讀取輸出流釋放緩沖,  不加這一句,進程會一直無限等待
process.WaitForExit();
}
}
catch (Exception e)
{
Debug.Log("Exception !!!");
}

return output;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM