我想使用以下路径启动一个进程。
"ProgramFiles(x86)\Philips Speech\Device Control Center PDCC.exe"
当我将其输入控制台时,进程会按预期启动,但当我尝试在代码中执行此操作时,出现以下异常:
the system cannot find the file specified
到目前为止,这是我的代码:
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
是否有人知道我是否可以通过为 ProcessStartInfo 类提供环境变量来直接执行此操作,或者我是否必须在执行此操作之前对其进行解析?
最佳答案
string path = Environment.ExpanEnvironmentVariables("%ProgramFiles(x86)%\Philips Speech\Device Control Center PDCC.exe");
var startInfo = new ProcessStartInfo(path);
new Process(startInfo).Start();
这样您就可以使用变量(例如 "%ProgramFiles(x86)%
)而不依赖于 C:\
中的文件夹什么的。
关于c# - 使用环境变量启动进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39166722/