Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 438
Thalia Avatar asked Dec 31 '25 05:12

Thalia


2 Answers

Take a look at these Stack Overflow questions:

  • C# - How to get Program Files (x86) on Windows Vista 64 bit
  • How do I programmatically retrieve the actual path to the “Program Files” folder?.
like image 163
KF2 Avatar answered Jan 02 '26 19:01

KF2


You can get it using following,

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
like image 36
Rajesh Subramanian Avatar answered Jan 02 '26 17:01

Rajesh Subramanian