Home >Backend Development >C++ >How to Programmatically Determine the Program Files (x86) Location in 64-bit Windows?
In an attempt to detect the presence of an application, you're utilizing `FileInfo(
System.Environment.GetFolderPath( System.Environment.SpecialFolder.ProgramFiles) + @"\MyInstalledApp"
)`. While this approach is adequate for 32-bit Windows versions, it encounters issues in x64 Windows Vista, where it retrieves the x64 Program Files folder instead of the intended Program Files (x86) location.
To resolve this, we need to find a programmatic method to retrieve the path to Program Files (x86) without resorting to hardcoding "C:Program Files (x86)".
The following function aptly fulfills this requirement:
static string ProgramFilesx86() { if( 8 == IntPtr.Size || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432")))) { return Environment.GetEnvironmentVariable("ProgramFiles(x86)"); } return Environment.GetEnvironmentVariable("ProgramFiles"); }
This function reliably returns the x86 Program Files directory in three distinct scenarios:
The above is the detailed content of How to Programmatically Determine the Program Files (x86) Location in 64-bit Windows?. For more information, please follow other related articles on the PHP Chinese website!