Search both ProgramFiles and ProgramFiles(x86) using environment variables in C++ and C# / VB.NET
Solution 1
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?.
Solution 2
You can get it using following,
Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Thalia
Junior developer exploring image processing and driver development
Updated on June 04, 2022Comments
-
Thalia almost 2 years
Possible Duplicate:
C# - How to get Program Files (x86) on Windows Vista 64 bitI am trying to launch a third-party program from my own. I have done a quick search in
Program Files
andProgram Files (x86)
, and I just realized that the path returned bygetenv("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...
-
KF2 almost 12 yearsaccording System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) returns "c:\Program Files" on a 64-bit machine, unless the code is build to target x86, in which case it returns "C:\Program Files (x86)".
-
Thalia almost 12 yearsI'll try it shortly, any idea how to do this in c++ ?
-
KF2 almost 12 yearsFor program files: TCHAR pf[MAX_PATH]; SHGetSpecialFolderPath( 0, pf, CSIDL_PROGRAM_FILES, FALSE );
-
KF2 almost 12 yearsThese two lines will get the required directories: string prog32 = Environment.GetEnvironmentVariable("ProgramFiles"); string prog64 = Environment.GetEnvironmentVariable("ProgramW6432"); results: C:\Program Files (x86) and C:\Program Files