简体   繁体   中英

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. How can I get the Process.Start to expand the environment variable in the file name?

The Environment.ExpandEnvironmentVariables method should help here.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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