22

Possible Duplicate:
C# - How to get Program Files (x86) on Windows Vista 64 bit

I realize the odds of a user changing the Windows default of C:\Program Files is fairly slim, but stranger things have happened!

How can I get the correct path to Program Files from the system?

Community
  • 1
  • 1
brasskazoo
  • 76,030
  • 23
  • 64
  • 76
  • 12
    Odds ore not that slim - on Spanish version the default path is C:\Archivos de programa\ That's why any sane developer should retrieve the path form the system, not hardcode it. – sharptooth Jul 06 '09 at 05:56
  • 1
    ...and for the Swedish version the default path is c:\program\. Definitely avoid hardcoding. – lemonad Jul 06 '09 at 06:11
  • 1
    And in Italian it's C:\Programmi – M.Turrini Jul 06 '09 at 07:03

6 Answers6

35

.NET provides an enumeration of 'special folders' for Program Files, My Documents, etc.

The code to convert from the enumeration to the actual path looks like this:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

http://msdn.microsoft.com/en-us/library/14tx8hby.aspx

brasskazoo
  • 76,030
  • 23
  • 64
  • 76
  • 5
    There is a gotcha to this if I recall correctly: it does not distinguish between x64 and x86 versions of Windows. If you're looking for %programfiles(x86)%, you'll have to find another method. – Jimmy Jul 06 '09 at 06:28
  • 3
    Indeed! There is a related question that covers the x64 issue: http://stackoverflow.com/questions/194157/c-how-to-get-program-files-x86-on-vista-x64 – brasskazoo Jul 06 '09 at 06:35
  • 3
    On a 64-bit OS, this depends on the platform you compile for. E.g. if you compile for AnyCPU or x64, this will give you C:\Program Files. If you compile for x86, this will give you C:\Program Files (x86). – Helen Jul 06 '09 at 07:06
28

Just to add to this.

If you're running in 32 bit mode (even on a 64 bit os), SpecialFolder.ProgramFiles and %PROGRAMFILES% will return ..Program Files (x86).

If you specifically need one and/or the other then you'll need to check as follows:

32 bit system:

SpecialFolder.ProgramFiles = ..Program Files\

64 bit system in 32 bit process: SpecialFolder.ProgramFiles = ..Program Files (x86)\ Environment.GetEnvironmentVariable("ProgramW6432") = ..Program Files\

64 bit system in 64 bit process: SpecialFolder.ProgramFiles = ..Program Files\ Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") = ..Program Files (x86)\

Obviously this depends on your locale etc...

John B
  • 1,129
  • 14
  • 23
  • 1
    Thank you! Just as a follow up (because I needed that), there is also the CommonProgramW6432 environment variable for the common files folder. – testalino May 08 '14 at 08:42
5

You would use GetFolderPath in the Environment class.

try {
    Environment.GetFolderPath( Environment.SpecialFolder.ProgramFiles )
catch( ArgumentException ex ) {
    Console.Out.WriteLine( ex.StackTrace );
}
Huppie
  • 11,263
  • 4
  • 32
  • 34
2

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) is probably the best solution, but another possible variant is evaluating the value of the ProgramFiles environment variable. For this, you can use the GetEnvironmentVariable or ExpandEnvironmentVariables method of the Environment class:

Environment.GetEnvironmentVariable("ProgramFiles")

Environment.ExpandEnvironmentVariables("%ProgramFiles%")
Helen
  • 87,344
  • 17
  • 243
  • 314
1

You can access the environment variable called: %PROGRAMFILES%

i.e:

%PROGRAMFILES%\Maxis\SimCity

in C#:

System.Environment.SpecialFolder.ProgramFiles
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
  • A correction: It should be %PROGRAMFILES%\Maxis\SimCity. The %PROGRAMFILES% variable already contains the 'C:\'. – Merus Jul 06 '09 at 05:54
1
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

gets "program files (x86)" in 64-bits Windows and "program files" in 32 bit.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Carl Hörberg
  • 5,973
  • 5
  • 41
  • 47