木子屋 Dnawo's BLOG

C#执行exe三种方法

👤 dnawo 📅 2019-11-07 👁 5314 👍 0 💬 0 🔄 来源:本站原创
第一种方法(静态方法):

System.Diagnostics.Process.Start("cmd.exe");

第二种方法(静态方法):

System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo("cmd.exe");
info.FileName = "cmd.exe";
info.WorkingDirectory = @"C:\";
System.Diagnostics.Process.Start(info);

第三种方法(实例方法):

System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C dir";
process.StartInfo.WorkingDirectory = @"C:\";
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
if(!process.WaitForExit(5000)) process.Kill(); //进程已退出再Kill会出错
Console.WriteLine(process.StandardOutput.ReadToEnd());

评论(0)

暂无评论。

验证码
评论需审核通过后显示
← 上一篇 C#远程桌面控件AxMsRdpClientNotSafeForS… 下一篇 → 2020年1月1日起货车上高速也可以走ETC车道