C# Winforms 和命令行批处理文件

4

我在我的C#窗体应用程序中运行了这个代码:

string ExecutableFilePath = @"Scripts.bat";
string Arguments = @"";

if (File.Exists(ExecutableFilePath )) {
    System.Diagnostics.Process.Start(ExecutableFilePath , Arguments);
}

当程序运行时,我能看到命令行窗口直到程序完成。
有没有一种方法可以在不显示给用户的情况下运行程序?
1个回答

9

您应该使用 ProcessStartInfo类,并设置以下属性

  string ExecutableFilePath = @"Scripts.bat";
  string Arguments = @"";

  if (File.Exists(ExecutableFilePath ))
  {
       ProcessStartInfo psi = new ProcessStartInfo(ExecutableFilePath , Arguments);
       psi.UseShellExecute = false;
       psi.CreateNoWindow = true;
       Process.Start(psi);
  }

好的,好的,我不会再作弊了 - 我会删除我的回答并给你点赞 :) - Matt Roberts
@MattRoberts,实际上我取消了我的踩票,但是以后这样做是不公平的,只是为了让你的答案先出现而随便写点什么 :) - Sergey Berezovskiy
那很好。谢谢。最后一个问题,有没有办法知道进程何时/是否已经执行完毕? - sd_dracula
Process.WaitForExit()将会阻止当前线程,直到该进程退出。 - Blam
没有在进程智能感知下得到那个。 - sd_dracula
明白了。需要先创建新进程,然后调用 p.WaitForExit()。谢谢。 - sd_dracula

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接