【问题标题】:System.Diagnostics.Process(); StartInfo.Arguments use Environment Variables as argumentSystem.Diagnostics.Process(); StartInfo.Arguments 使用环境变量作为参数
【发布时间】:2016-01-22 00:59:35
【问题描述】:

如何将环境变量作为参数传递给 System.Diagnostics.Process()?由于某种原因,使用可变路径失败。 例如,我试图在路径 %windir% 处打开资源管理器,但失败:

程序:explorer.exe 参数:/n,/e,%windir%

var f = new System.Diagnostics.Process();
f.StartInfo.WorkingDirectory = Path.GetDirectoryName(Program);
f.StartInfo.FileName = Program;
f.StartInfo.Arguments = !string.IsNullOrWhiteSpace(Params) ? Params : null;
f.Start();

【问题讨论】:

标签: c# environment-variables system.diagnostics


【解决方案1】:

正如评论者 Hans Passant 所说,%windir% 之类的语法特定于命令行处理器。您可以通过调用Environment.GetEnvironmentVariable("windir")(即获取WINDIR 环境变量的当前值)或Environment.GetFolderPath(SpecialFolder.Windows)(即让Windows 报告已知特殊文件夹的路径)在您自己的代码中模拟它。

如果您想让命令行处理器完成工作,您需要运行命令行处理器。例如:

f.StartInfo.FileName = "cmd.exe";
f.StartInfo.Arguments = "/c explorer.exe /n /e /select,%windir%";

这将运行cmd.exe,然后它将代表您启动explorer.exe 进程,将%windir% 表达式解析为环境变量取消引用。

【讨论】:

    猜你喜欢
    • 2012-02-03
    • 2019-09-02
    • 2017-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多