tags:

views:

526

answers:

4

Queries I tried: ExpandEnvironmentStrings("%COMMONPROGRAMFILES%"), GetSpecialPath(CSIDL_PROGRAM_FILES_COMMON).

All resolve to (typically) c:\\Program Files (x86)\\Common Files from my 32 bit app. I need to check a file version installed (typically) under c:\\Program Files\\Common Files of a 643 bit application.

A: 
static string ProgramFilesx86()
        {
            if (8 == IntPtr.Size
                || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }

Is this what you're needing?

Dremation
That sure does not look like C++. Maybe C++/CLI but I'd bet C# given a guess.
Billy ONeal
I could have swore I saw C# on the tags. Sorry.
Dremation
+2  A: 

%CommonProgramW6432%

Kyle Alons
Thanks! Somehow I missed that in the list of env variables.
peterchen
+2  A: 

On 64-bit operating systems, the ProgramW6432 environment variable points to c:\program files. The full list for a 32-bit app on an English version of Windows:

  • ProgramFiles => c:\program files (x86)
  • ProgramFiles(x86) => c:\program files (x86)
  • ProgramW6432 => c:\program files
  • CommonProgramFiles => c:\program files (x86)\common files
  • CommonProgramFiles(x86) => c:\program files (x86)\common files
  • CommonProgramW6432 => c:\program files\common files

Just a reminder: that folder should not contain anything of interest to a 32-bit program. Technically.

Hans Passant
I am just checking that a required (64 bit) application is already installed.
peterchen
A: 
HRESULT hr;
PWSTR pwszFolder = NULL;
hr = SHGetKnownFolderPath(FOLDERID_ProgramFilesX64, 0, NULL, &pwszFolder));

if (SUCCEEDED(hr))
{
   // your code goes here
}

CoTaskMemFree(pwszFolder);

Alternatively, you can use "FOLDERID_ProgramFiles" as the param to SHGetKnownFolderPath and it will return the approprate folder for both 64-bit and 32-bit process.

selbie