繁体   English   中英

的System.Diagnostics.Process(); StartInfo.Arguments使用环境变量作为参数

[英]System.Diagnostics.Process(); StartInfo.Arguments use Environment Variables as argument

如何将环境变量作为参数传递给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();

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

如果要让命令行处理器执行此工作,则需要运行命令行处理器。 例如:

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

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

暂无
暂无

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

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