4

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();
Diego
  • 16,436
  • 26
  • 84
  • 136
  • Possibly what you need: http://stackoverflow.com/questions/4348003/using-environment-variable-in-a-file-path – Alex Mar 12 '12 at 22:03
  • See http://stackoverflow.com/questions/1013383/how-can-an-application-access-the-environment-variable-set-by-another-applicatio Process start does not expand environment variables. – Chriseyre2000 Mar 12 '12 at 22:03
  • I haven't found it before posting (I've been searching for a while) – Diego Mar 12 '12 at 22:06

5 Answers5

4

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");
casperOne
  • 73,706
  • 19
  • 184
  • 253
1

The %temp% portion of the string is being interpreted literally instead of being replaced with the appropriate environment variable. You'll need to manually expand it

var temp = Environment.GetEnvironmentVariable("temp");
var fileName = Path.Combine(temp, "SSCERuntime_x86-ENU.msi");
p.StartInfo.FileName = fileName;
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

%TEMP% is parsed and evaluated by Command Shell. You could use use Path.GetTempPath() and Path.Combine for this purpose.

p.StartInfo.FileName = Path.Combine(Path.GetTempPath(), @"SSCERuntime_x86-ENU.msi");
amit_g
  • 30,880
  • 8
  • 61
  • 118
  • Great!! Thanks! BTW: I changed string.Concat for Path.Combine, I think it is more appropriate here. – Diego Mar 12 '12 at 22:05
  • 1
    Path.Combine is almost always more appropriate than string.Concat when you are dealing with file or path names; it performs all of the path (and drive) separator logic for you, then does a string.Contact anyway. – Michael Edenfield Mar 12 '12 at 22:08
0

Try this:

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

Then concat it in:

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

Casper beat me to the punch on the explaination, but the Process.Start method basically treats it literally instead of intrperting it as the shell would.

Todd Richardson
  • 1,119
  • 1
  • 11
  • 22
0

You can use the Environment.ExpandEnvironmentVariables to expand environment variables within a string, then pass that to the Process class:

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

This has the added benefits of

  1. Working for any environment variable (%APPDATA%, %COMMONPROGRAMFILES%, etc), and
  2. Working anywhere in the string (e.g. "%temp%\%username%\foo.txt")
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117