dbostream
dbostream

Reputation: 811

Retrieve x64 Program Files path from 32-bit process

First some background. I have created a WPF bootstrapper application for my WiX installer where I need to retrieve and display the default install location. Unless I am doing something wrong it appears the executable created by WiX is always 32-bit even if my Visual Studio configuration is set to x64 and this leads to my problem.

Since the executable is always 32-bit the library containing my WPF bootstrapper application is also loaded as 32-bit even when I want to install my x64 MSI-file. So when I try to retrieve the default installation path using Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) I get C:\Program Files (x86) instead of C:\Program Files. How would I go about retrieving C:\Program Files from a 32-bit process when running on 64-bit OS?

I have found similar questions but I have not found an adequate answer, I want a solution that works even if the OS language is not English. I was thinking about removing " (x86)" from the retrieved path but it seems hacky and I am not sure if it will work in all situations.

Upvotes: 4

Views: 1606

Answers (1)

Matteo Umili
Matteo Umili

Reputation: 4017

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

Upvotes: 7

Related Questions