Dr. Fre Dr. Fre - 4 months ago 87
C# Question

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?

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.