C# 運行cmd命令的代碼
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics;namespace ConsoleApplication1 { class command { public static string startcmd(string command) { string output = ""; try {
Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); Console.WriteLine(output); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { Console.WriteLine(e); } return output; } public static string startcmd(string command, string argument) { string output = ""; try { Process cmd = new Process(); cmd.StartInfo.FileName = command; cmd.StartInfo.Arguments = argument; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); output = cmd.StandardOutput.ReadToEnd(); Console.WriteLine(output); cmd.WaitForExit(); cmd.Close(); } catch (Exception e) { Console.WriteLine(e); } return output; } }
}
</pre>
本文由用戶 xf3f 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!