使用 Process.Start 启动 WPF 应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19969693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:03:16  来源:igfitidea点击:

Launch WPF application using Process.Start

c#wpfprocess

提问by Christian Stewart

I am attempting to launch a wpf applicationusing Process.Start. When I launch the process by double-clicking it in explorer.exe, it launches properly; however, when I try to use the following code snippet:

我试图推出一个WPF应用程序使用Process.Start。当我通过双击启动进程时explorer.exe,它会正常启动;但是,当我尝试使用以下代码片段时:

var programPath = @"C:\Users\user\Documents\Program Directory\program.exe";
if(!File.Exists(programPath))
{
     MessageBox.Show("The program.exe file does not exist! Cannot launch.");
     return;
}
Process.Start(programPath);

My WPF process flashes in the task manager briefly before immediately closing.

我的 WPF 进程在立即关闭之前在任务管理器中短暂闪烁。

回答by Christian Stewart

I fixed the problem this way:

我这样解决了这个问题:

Process proc = new Process();
proc.StartInfo.FileName = programPath;
proc.StartInfo.WorkingDirectory = Path.GetDirectoryName(programPath);
proc.Start();

The trick was to set the working directory to the path of the WPF application, rather than the working directory of the launching application.

诀窍是将工作目录设置为 WPF 应用程序的路径,而不是启动应用程序的工作目录。