views:

331

answers:

2

In C#, show to refer the locations:

  • Program Files (x86)
  • Program Files

rather than hard-coding C:\Program Files (x86) and C:\Program Files

+12  A: 

You can use:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

to get the program files folder on the machine. This will return what ever is correct for the app that is running. So for 32 bit apps on a 64 bit machine it will return "program files (x86)"

Simon P Stevens
This will also cope with internationalised versions of windows
Rowland Shaw
+2  A: 

These two C# lines will get the required directories:

string f32 = Environment.GetEnvironmentVariable("ProgramFiles");

string f64 = Environment.GetEnvironmentVariable("ProgramW6432");

Typical values are

C:\Program Files (x86)

and

C:\Program Files

for f32 and f64 respectively.

Peter Mortensen
My Vista64 machine doesn't have those environment variables.
RichieHindle
@RichieHindle: have you tried to run the code? ProgramW6432 is not present for command line windows.
Peter Mortensen