C# - How to get Program Files (x86) on Windows 64 bit
Solution 1
The function below will return the x86 Program Files
directory in all of these three Windows configurations:
- 32 bit Windows
- 32 bit program running on 64 bit Windows
- 64 bit program running on 64 bit windows
static string ProgramFilesx86()
{
if( 8 == IntPtr.Size
|| (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
}
return Environment.GetEnvironmentVariable("ProgramFiles");
}
Solution 2
If you're using .NET 4, there is a special folder enumeration ProgramFilesX86:
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Solution 3
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Solution 4
Note, however, that the ProgramFiles(x86)
environment variable is only available if your application is running 64-bit.
If your application is running 32-bit, you can just use the ProgramFiles
environment variable whose value will actually be "Program Files (x86)".
Solution 5
One way would be to look for the "ProgramFiles(x86)" environment variable:
String x86folder = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
Related videos on Youtube
Leonard H. Martin
Updated on December 26, 2020Comments
-
Leonard H. Martin over 3 years
I'm using:
FileInfo( System.Environment.GetFolderPath( System.Environment.SpecialFolder.ProgramFiles) + @"\MyInstalledApp"
In order to determine if a program is detected on a users machine (it's not ideal, but the program I'm looking for is a right old kludge of a MS-DOS application, and I couldn't think of another method).
On Windows XP and 32-bit versions of Windows Vista this works fine. However, on x64 Windows Vista the code returns the x64 Program Files folder, whereas the application is installed in Program Files x86. Is there a way to programatically return the path to Program Files x86 without hard wiring "C:\Program Files (x86)"?
-
VitalyB about 12 yearsIt is worth noting that this returns the "Program files" only in 64bit application on 64bit OS. If you compile your application specifically as x86 then it would return "Program files (x86)" on 64bit OS using this code.
-
-
tomasr over 15 yearsTrue enough. However, it is obvious that his application is running as 32-bit, otherwise GetFolderPath() would already be returning the right x86 folder anyway.
-
David Silva Smith about 14 yearsVery helpful! this just saved me hours of work! and saved people from having to use my code. It's great when things work out of the box!
-
Tom almost 14 yearsCan anyone comment as to whether this works in a localized environment? Thanks in advance...
-
Marcel Gosselin about 13 yearsHow does this behave on a 32 bit OS? Does it return "c:\Program files" without x86?
-
Nathan about 13 yearsYes. It will return c:\program files on x86 and c:\program files (x86) on 64-bit windows.
-
Florian over 12 yearsWhy this test : 8 == IntPtr.Size ?
-
JaredPar over 12 years@Florian it's a test to check for a 64 bit process
-
Arifa Raj over 12 yearsTest it yourself - on Server 2003 32 bit, this returns empty string for me: Console.WriteLine("X86:" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86));
-
Kiquenet over 12 yearsGood reference about x64-x86: rongchaua.net/blog/…
-
Patrick McDonald about 12 yearsAlso returns empty string on Windows XP (32 bit)
-
dwp4ge over 10 yearsMicrosoft's documentation states: " On an x86 system, passing the ProgramFilesX86 member to the Environment.GetFolderPath method returns String.Empty; use the ProgramFiles member instead. You can determine whether Windows is a 32-bit operating system by calling the Environment.Is64BitOperatingSystem property."
-
osvein over 10 yearsWhy not just
Environment.Is64BitOperatingSystem
orEnvironment.Is64BitProcess
? -
Rotem about 10 years@anustart This was answered in 2008, before those methods were available in .NET 4.0.
-
DiscipleMichael almost 9 yearsReally, this should be marked as an acceptable answer too. This is much better than performing logic tests. However, it will return the folder appropriate for the compiled bit-type of the app. Meaning, if you compiled the app in 32bit, it will return "Program Files" on a 32bit OS and "Program Files (x86)" on a 64bit OS. PERFECT!
-
tedebus over 6 yearsBe careful: the question is "How to get Program Files (x86) on Windows 64 bit" and not "How to get the right Program File folder of the running application". So this answer, that is right in terms of functionality, is not a relevant answer to the question.