C# - How to Get Program Files (X86) on Windows 64 Bit

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

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");
}

How to get ProgramFiles paths?

The result depends on what platform is your project targeting. If you target x86, then both Environment.SpecialFolder.ProgramFiles and Environment.SpecialFolder.ProgramFilesX86 will return the same path.

Retrieve x64 Program Files path from 32-bit process

If OS is 64 bit and your application is 32 bit you can get Program Files folder using an environment variable (actually there is a special - unsupported in Environment.SpecialFolder - constant in SHGetSpecialFolderLocation but it's easier like this):

Environment.GetEnvironmentVariable("ProgramW6432");

Just check your OS is 64 bit (in 32 bit systems this variable isn't defined):

if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
return Environment.GetEnvironmentVariable("ProgramW6432");
else
return Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

How do I programmatically retrieve the actual path to the Program Files folder?

.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

What does the ProgramFilesX86 SpecialFolder do on systems like Windows XP?

It returns "Program Files". Since you're installing using an MSI, you might consider using the installer APIs (MsiLocateComponent, and so on) to locate your program instead of assuming it's in the expected location.

Path of %ProgramFiles(x86)% in 64 bit machine (for Registry)

If your installer is marked x64, you can use the ProgramFilesFolder installer property:

"[ProgramFilesFolder]MyApp\MyApp.exe" "%1"

In x64 mode, this property will point to the x86 Program Files folder, and ProgramFiles64Folder will point to the x64 Program Files folder.

EDIT: If you import a reg file into the registry instead of having the installer generate the keys and values, you can use an environment variable instead:

"%ProgramFiles(x86)%\MyApp\MyApp.exe" "%1"

How do get the path of Program Files regardless of the architecture of the target machine

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) returns "c:\Program Files" on a 64-bit machine, unless the code is build to target x86, in which case it returns "C:\Program Files (x86)", so I guess that would work for you.

Update DLLs in the Program Files (x86) Folder

You can add Application Manifest File to your project and replace

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

with

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

This way your application will require administrator rights to run.

But better yet, consider following approach. Install launcher into Program Files and use it to download the latest version (of application or launcher) to the AppData folder and launch it from there - no admin rights required.



Related Topics



Leave a reply



Submit