【问题标题】:Process start but don't show the window进程启动但不显示窗口
【发布时间】:2015-12-15 16:16:56
【问题描述】:

我正在尝试打开一个外部可执行文件。我可以使用System.Diagnostics 中的Process 类轻松打开外部可执行文件:

Process p = new Process()
p.StartInfo.FileName = processName;
p.Start();

这适用于大多数程序,如浏览器和记事本,创建一个进程并显示其图形界面。但是,当我尝试打开我想要的程序时,该进程已启动(可以在任务管理器中看到它,它甚至需要整个 CPU 内核进行处理)但 GUI 窗口不显示。 Process.Start 中的进程不显示其 GUI 可能会发生什么情况?

作为参考,我要执行的程序是ADOM release 60,当我直接在 Explorer shell 或 Powershell 中打开它时,它运行 100% 正常。这在控制台和 WindowsForms 应用程序中都可以重现。

以下是一些没有帮助的其他设置:

p.StartInfo.CreateNoWindow = true; // or false
p.StartInfo.ErrorDialog = false;
p.StartInfo.WindowStyle = /* any of possible values */;
p.StartInfo.UseShellExecute = true; // or false

【问题讨论】:

  • 远射,但它可能需要一些命令行参数吗?
  • @NateBarbettini 不,我可以通过调用名称 .\adom.exe 在 Powershell 上正常执行它。
  • 进程是否与您的程序命名空间共享名称?另外,它不是只在调试器中工作吗?
  • 您似乎缺少一些关于以下内容的代码,例如我希望看到process.StartInfo.FileName = processName; process.StartInfo.Arguments = commandLineArgs; process.StartInfo.CreateNoWindow = true; process.StartInfo.ErrorDialog = false; process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
  • @TheApache 不做巫毒教/货物崇拜编程。将属性设置为其记录的默认值是没有用的。

标签: c# process


【解决方案1】:

您需要将WorkingDirectory设置为可执行文件所在的根文件夹。

var executablePath = .....;
var p = new Process();
p.StartInfo = new ProcessStartInfo(executablePath);
p.StartInfo.WorkingDirectory = Path.GetDirectoryName(executablePath);
p.Start();

WorkingDirectory 的默认值为%SYSTEMROOT%\System32。许多遗留应用程序具有硬编码的相对路径,这些路径会解析为具有所述目录的完整路径,并且在尝试读取(或创建)不应该读取的文件时会失败。

【讨论】:

  • 没有让它工作。我唯一的新效果是,我的程序中的控制台窗口在我关闭表单后不会自动关闭。
猜你喜欢
  • 2010-12-04
  • 1970-01-01
  • 2019-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多