使用%TEMP%的Process.Start文件名 [英] Process.Start filename using %temp%

查看:152
本文介绍了使用%TEMP%的Process.Start文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一些奇怪的reaseon验证码失败:

  p.StartInfo.FileName = @%TEMP%\SSCERuntime_x86- ENU.MSI; 

和验证码更迭:

  p.StartInfo.FileName = @C:\Users\USERNAME\AppData\Local\Temp\SSCERuntime_x86-ENU.MSI 



是否有任何理由我失踪?



注意我刚才复制的路径,我不认为代码的其余部分是必要的,但我会把它无论如何:

 进程p =新工艺(); 
p.StartInfo.FileName = @%temp%\SSCERuntime_x86-ENU.MSI
p.StartInfo.Arguments =/被动;
p.Start(); // MSDN:


解决方案

过程不扩大与环境字符串变量(即%TEMP%)。



如果你想使用环境变量来构建的 文件名属性那么你就必须获得环境变量(使用 GetEnvironmentVariable href=\"http://msdn.microsoft.com/en-us/library/system.environment.aspx\" rel=\"nofollow\"> 环保),并自己进行替换,就像这样:

  //构建的路径。 
字符串TEMP = Environment.GetEnvironmentVariable(TEMP);
路径字符串= Path.Combine(TEMPSSCERuntime_x86-ENU.MSI);

//启动过程。
进程p =新工艺();
p.StartInfo.FileName =路径;
p.StartInfo.Arguments =/被动;
p.Start();

此外,可以使用的 ExpandEnvironmentVariables 方法与你的原始字符串像这样

  p.StartInfo.FileName = 
Environment.ExpandEnvironmentVariables(@%temp%\SSCERuntime_x86-ENU.MSI);


For some odd reaseon this code fails:

p.StartInfo.FileName = @"%temp%\SSCERuntime_x86-ENU.msi";

and this code succes:

p.StartInfo.FileName = @"C:\Users\USERNAME\AppData\Local\Temp\SSCERuntime_x86-ENU.msi";

Is there any reason I am missing?

Note I just copied the path, I don't think the rest of the code is needed but I'll put it anyway:

Process p = new Process();
p.StartInfo.FileName = @"%temp%\SSCERuntime_x86-ENU.msi";
p.StartInfo.Arguments = "/passive";
p.Start();

解决方案

The Process class does not expand strings with environment variables (i.e. %temp%).

If you want to use environment variables to build the FileName property then you'll have to get the environment variables (using the GetEnvironmentVariable method on the Environment class) and perform the substitution yourself, like so:

// Construct the path.
string temp = Environment.GetEnvironmentVariable("temp");
string path = Path.Combine(temp, "SSCERuntime_x86-ENU.msi");

// Launch the process.
Process p = new Process();
p.StartInfo.FileName = path;
p.StartInfo.Arguments = "/passive";
p.Start();

Additionally, you can use the ExpandEnvironmentVariables method with your original string like so:

p.StartInfo.FileName = 
    Environment.ExpandEnvironmentVariables(@"%temp%\SSCERuntime_x86-ENU.msi");

这篇关于使用%TEMP%的Process.Start文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆