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

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


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

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)


Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)