c# - 使用环境变量启动进程

标签 c# process environment-variables

我想使用以下路径启动一个进程。

"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/

相关文章:

c - tcp消息通信

spring - 将环境变量传递给 DOCKER Spring Boot

javascript - React - 开发和生产中的环境变量

c# - 在 c# net 3.5 下的 Win7 x64 上,在标题中添加图标进行排序很奇怪

powershell - 将参数传递给Powershell Start-Job

linux - Linux 中进程的线程堆栈

terminal - 如何将环境变量 PACKAGE_DIRS 链接到我的本地私有(private)包?

c# - 依赖注入(inject)和项目引用

c# - 是否有通过泛型类使用静态方法的解决方法?

c# - 如何检查类型是否是可移植库中的结构