Process.Start filename using% temp%

For some odd reaseon, this code doesn't work:

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

      

and this code succeeds:

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

      

Is there some 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();

      

+3


source to share


5 answers


Process

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

).

If you want to use environment variables to create a property FileName

, you will need to get the environment variables (using GetEnvironmentVariable

in Environment

class
) and execute replace yourself like this:

// 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();

      



Alternatively, you can use the method ExpandEnvironmentVariables

with your original string, for example:

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

      

+4


source


The %temp%

string is interpreted literally instead of replacing the corresponding environment variable. You will need to manually deploy it



var temp = Environment.GetEnvironmentVariable("temp");
var fileName = Path.Combine(temp, "SSCERuntime_x86-ENU.msi");
p.StartInfo.FileName = fileName;

      

+1


source


% TEMP% is parsed and evaluated by the shell. You can use Path.GetTempPath () and Path.Combine for this purpose.

p.StartInfo.FileName = Path.Combine(Path.GetTempPath(), @"SSCERuntime_x86-ENU.msi");

      

0


source


Try the following:

string tempPath = Environment.GetEnvironmentVariable("Temp");

      

Then split it into:

p.StartInfo.FileName = Path.Combine(tempPath, "SSCERuntime_x86-ENU.msi"); 

      

Casper beat me to a blow at the explanation, but the Process.Start method basically treats it literally instead of sticking it into a shell.

0


source


You can use Environment.ExpandEnvironmentVariables

to expand environment variables inside a string and then pass that to the class Process

:

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

      

This has additional benefits

  • Working with any environment variable (% APPDATA%,% COMMONPROGRAMFILES%, etc.) and
  • Work anywhere in the line (eg "% temp% \% username% \ foo.txt")
0


source







All Articles