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

8

156

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

Azeotrope answered 11/10, 2008 at 14:53 Comment(1)
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.Lawson
S
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");
}
Seismograph answered 11/10, 2008 at 15:45 Comment(6)
Can anyone comment as to whether this works in a localized environment? Thanks in advance...Rathe
Why this test : 8 == IntPtr.Size ?Literate
@Literate it's a test to check for a 64 bit processSeismograph
Good reference about x64-x86: rongchaua.net/blog/…Xylo
Why not just Environment.Is64BitOperatingSystem or Environment.Is64BitProcess?Schulz
@anustart This was answered in 2008, before those methods were available in .NET 4.0.Appendix
D
146

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

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)
Doggy answered 22/12, 2010 at 21:58 Comment(5)
How does this behave on a 32 bit OS? Does it return "c:\Program files" without x86?Calculator
Yes. It will return c:\program files on x86 and c:\program files (x86) on 64-bit windows.Doggy
Test it yourself - on Server 2003 32 bit, this returns empty string for me: Console.WriteLine("X86:" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86));Halftimbered
Also returns empty string on Windows XP (32 bit)Lett
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."Leotie
F
43
Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Finny answered 14/12, 2010 at 18:2 Comment(0)
C
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)".

Callipygian answered 11/10, 2008 at 15:19 Comment(2)
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.Frederic
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!Melatonin
F
9

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

String x86folder = Environment.GetEnvironmentVariable("ProgramFiles(x86)");
Frederic answered 11/10, 2008 at 14:59 Comment(0)
B
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")
Birdt answered 28/2, 2011 at 9:26 Comment(2)
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!Retrospection
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.Lascar
M
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"))

Mcguire answered 13/6, 2018 at 16:28 Comment(0)
T
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

Topknot answered 25/12, 2019 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.