C# - How to get Program Files (x86) on Windows 64 bit

152,268

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)");
Share:
152,268

Related videos on Youtube

Leonard H. Martin
Author by

Leonard H. Martin

Updated on December 26, 2020

Comments

  • Leonard H. Martin
    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
      VitalyB about 12 years
      It 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
    tomasr over 15 years
    True 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
    David Silva Smith about 14 years
    Very 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
    Tom almost 14 years
    Can anyone comment as to whether this works in a localized environment? Thanks in advance...
  • Marcel Gosselin
    Marcel Gosselin about 13 years
    How does this behave on a 32 bit OS? Does it return "c:\Program files" without x86?
  • Nathan
    Nathan about 13 years
    Yes. It will return c:\program files on x86 and c:\program files (x86) on 64-bit windows.
  • Florian
    Florian over 12 years
    Why this test : 8 == IntPtr.Size ?
  • JaredPar
    JaredPar over 12 years
    @Florian it's a test to check for a 64 bit process
  • Arifa Raj
    Arifa Raj over 12 years
    Test it yourself - on Server 2003 32 bit, this returns empty string for me: Console.WriteLine("X86:" + System.Environment.GetFolderPath(System.Environment.SpecialF‌​older.ProgramFilesX8‌​6));
  • Kiquenet
    Kiquenet over 12 years
    Good reference about x64-x86: rongchaua.net/blog/…
  • Patrick McDonald
    Patrick McDonald about 12 years
    Also returns empty string on Windows XP (32 bit)
  • dwp4ge
    dwp4ge over 10 years
    Microsoft'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
    osvein over 10 years
    Why not just Environment.Is64BitOperatingSystem or Environment.Is64BitProcess?
  • Rotem
    Rotem about 10 years
    @anustart This was answered in 2008, before those methods were available in .NET 4.0.
  • DiscipleMichael
    DiscipleMichael almost 9 years
    Really, 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
    tedebus over 6 years
    Be 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.