Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start process with environment variable

I want to start a process with the following path.

"ProgramFiles(x86)\Philips Speech\Device Control Center PDCC.exe"

When I type this into the console the process starts as expected but when I try to do it in code I get the following exception:

the system cannot find the file specified

This I my code so far:

var startInfo = new ProcessStartInfo("%ProgramFiles(x86)%\Philips Speech\Device Control Center PDCC.exe");
Debug.Assert(startInfo.EnvironmentVariables.ContainsKey("ProgramFiles(x86)")) //Is true
new Process(startInfo).Start(); //<- exception occures here

Does anybody have an idea if I can do this directly by giving the ProcessStartInfo class the environment variable or if I have to parse it before doing so?

like image 795
NtFreX Avatar asked Jul 19 '26 19:07

NtFreX


2 Answers

string path = Environment.ExpanEnvironmentVariables("%ProgramFiles(x86)%\Philips Speech\Device Control Center PDCC.exe");
var startInfo = new ProcessStartInfo(path);
new Process(startInfo).Start(); 

This way you can use the variables (e.g. "%ProgramFiles(x86)%) and not depend on the folder being in C:\ or something.

like image 200
ArgusMagnus Avatar answered Jul 21 '26 09:07

ArgusMagnus


You should use this to get the path to Program Files:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

Special folders enum.

like image 34
Quentin Roger Avatar answered Jul 21 '26 10:07

Quentin Roger



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!