154

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)"?

Mathieu Renda
  • 14,069
  • 2
  • 35
  • 33
Leonard H. Martin
  • 2,734
  • 4
  • 23
  • 26
  • 9
    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. – VitalyB Feb 08 '12 at 13:02

8 Answers8

235

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");
}
Vivek Jain
  • 3,811
  • 6
  • 30
  • 47
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
145

If you're using .NET 4, there is a special folder enumeration ProgramFilesX86:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Lee Grissom
  • 9,705
  • 6
  • 37
  • 47
Nathan
  • 1,583
  • 1
  • 9
  • 4
  • 2
    How does this behave on a 32 bit OS? Does it return "c:\Program files" without x86? – Marcel Gosselin Feb 28 '11 at 18:51
  • 6
    Yes. It will return c:\program files on x86 and c:\program files (x86) on 64-bit windows. – Nathan Mar 01 '11 at 20:33
  • 2
    Test it yourself - on Server 2003 32 bit, this returns empty string for me: Console.WriteLine("X86:" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86)); – David Eison Dec 07 '11 at 19:14
  • 4
    Also returns empty string on Windows XP (32 bit) – Patrick McDonald Feb 09 '12 at 12:13
  • 12
    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." – dwp4ge Oct 16 '13 at 17:40
43
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Carl Hörberg
  • 5,973
  • 5
  • 41
  • 47
14

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chadmyers
  • 3,800
  • 21
  • 29
  • 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. – tomasr Oct 11 '08 at 15:32
  • 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! – David Silva Smith Feb 05 '10 at 15:00
9

One way would be to look for the "ProgramFiles(x86)" environment variable:

String x86folder = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
tomasr
  • 13,683
  • 3
  • 38
  • 30
5

I am writing an application which can run on both x86 and x64 platform for Windows 7 and querying the below variable just pulls the right program files folder path on any platform.

Environment.GetEnvironmentVariable("PROGRAMFILES")
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Samir
  • 51
  • 1
  • 1
  • 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! – DiscipleMichael Jun 29 '15 at 17:17
  • 1
    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. – tedebus Oct 24 '17 at 13:58
0

One-liner using the new method in .NET. Will always return x86 Program Files folder.

Environment.Is64BitOperatingSystem ? Environment.GetEnvironmentVariable("ProgramFiles(x86)") : Environment.GetEnvironmentVariable("ProgramFiles"))

Red John
  • 167
  • 10
0

C# Code:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

Output:

C:\Program Files (x86)

Note:

We need to tell the compiler to not prefer a particular build platform.

Go to Visual Studio > Project Properties > Build > Uncheck "Prefer 32 bit"

Reason:

By default for most .NET Projects is "Any CPU 32-bit preferred"

When you uncheck 32 bit assembly will:

JIT to 32-bit code on 32 bit process

JIT to 32-bit code on 64 bit process

Community
  • 1
  • 1
Clint
  • 6,011
  • 1
  • 21
  • 28