Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch WPF application using Process.Start

Tags:

c#

process

wpf

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

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

like image 218
Christian Stewart Avatar asked Mar 20 '26 02:03

Christian Stewart


1 Answers

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.

like image 171
Christian Stewart Avatar answered Mar 21 '26 14:03

Christian Stewart



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!