c# - 如何确保 C# 的 Process.Start 会扩展环境变量?

标签 c# environment-variables filenames process.start

我正在尝试创建一个这样的流程:

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/

相关文章:

c# - WinSCP SFTP - .tmp 进程无法访问文件

c# - 将实时音频输入放入字节数组并作为 XMPP 消息发送时出现 OnStreamError

java - System.getenv() 环境变量存在时返回 null

shell - 创建与目标文件同名的软链接(soft link)

windows - ffmpeg 提取具有零索引文件名的帧

java - 检查下划线是否包含任何有效的 Windows 文件名字符(下划线和 csv 扩展名除外)

c# - 如何在 C++ 中声明类的静态实例?

c# - protobuf-net 序列化返回空数组

kotlin - 在 Kotlin/Native 中读取环境变量

c++ - VC++ 不使用 GetEnvironmentVariable 检测新创建的环境变量