Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I programmatically retrieve the actual path to the "Program Files" folder? [duplicate]

Tags:

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?

like image 242
brasskazoo Avatar asked Jul 06 '09 05:07

brasskazoo


2 Answers

.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

like image 104
brasskazoo Avatar answered Oct 21 '22 22:10

brasskazoo


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...

like image 25
John B Avatar answered Oct 21 '22 20:10

John B