我正在尝试创建一个这样的流程:
var psi = new ProcessStartInfo
{
FileName = @"%red_root%\bin\texturepreviewer.exe",
UseShellExecute = true
};
var process = Process.Start(psi);
process.WaitForExit();
现在环境变量“red_root”肯定存在于派生进程的环境变量中,但执行似乎没有扩展环境变量,因此找不到文件。如何获取Process.Start展开文件名中的环境变量?
最佳答案
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);
关于c# - 如何确保 C# 的 Process.Start 会扩展环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4388639/