简体   繁体   English

我如何确保C#的Process.Start将扩展环境变量?

[英]How do I ensure C#'s Process.Start will expand environment variables?

I'm attempting to create a process like so: 我正在尝试创建一个这样的过程:

var psi = new ProcessStartInfo
{
    FileName = @"%red_root%\bin\texturepreviewer.exe",
    UseShellExecute = true
};

var process = Process.Start(psi);
process.WaitForExit();

Now the environment variable "red_root" definitely exists in the spawned process' environment variables, but the execute doesn't seem to expand the environment variable and so the file isn't found. 现在环境变量“red_root”肯定存在于生成进程的环境变量中,但是执行似乎没有扩展环境变量,因此找不到该文件。 How can I get the Process.Start to expand the environment variable in the file name? 如何让Process.Start扩展文件名中的环境变量?

The Environment.ExpandEnvironmentVariables method should help here. Environment.ExpandEnvironmentVariables方法应该在这里提供帮助。

Replaces the name of each environment variable embedded in the specified string with the string equivalent of the value of the variable, then returns the resulting string. 将嵌入在指定字符串中的每个环境变量的名称替换为等效于变量值的字符串,然后返回结果字符串。

string unexpandedPath = "%red_root%\\bin\\texturepreviewer.exe";   
psi.FileName = Environment.ExpandEnvironmentVariables(unexpandedPath);

您是否尝试过System.Environment.GetEnvironmentVariable ("red_root", EnvironmentVariableTarget.Machine)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM