Search both ProgramFiles and ProgramFiles(x86) using environment variables in C++ and C# / VB.NET

Thalia picture Thalia · Jun 22, 2012 · Viewed 16.1k times · Source

Possible Duplicate:
C# - How to get Program Files (x86) on Windows Vista 64 bit

I am trying to launch a third-party program from my own. I have done a quick search in Program Files and Program Files (x86), and I just realized that the path returned by getenv("ProgramFiles") actually depends on whether I am running in x64 or Win32.

How can I search (both in C++ and C# or VB.NET) both Program Files folders, using environment variables and not hard-coded names - since regardless of the version of my program running on the user machine, the user might have the other one installed in a different version?

My code now: in C++:

fs::path root_directory = fs::path(getenv("ProgramFiles"));
// and then I change to 
root_directory = fs::path(getenv("ProgramFiles(x86)"));

in VB.NET:

System.Environment.GetEnvironmentVariable("ProgramFiles")

I looked at this source: http://msdn.microsoft.com/en-us/library/aa365743

But if I implement what they say, I get x86 all the time...