使用 Process.Start 启动 WPF 应用程序 [英] Launch WPF application using Process.Start

查看:58
本文介绍了使用 Process.Start 启动 WPF 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Process.Start 启动 wpf 应用程序.当我通过在 explorer.exe 中双击启动该进程时,它会正常启动;但是,当我尝试使用以下代码片段时:

I am attempting to launch a wpf application using 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:

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);

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

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

推荐答案

我是这样解决的:

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

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

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

这篇关于使用 Process.Start 启动 WPF 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆