3

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

Community
  • 1
  • 1
Thalia
  • 13,637
  • 22
  • 96
  • 190

2 Answers2

1

Take a look at these Stack Overflow questions:

Community
  • 1
  • 1
KF2
  • 9,887
  • 8
  • 44
  • 77
  • 1
    according 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)". – KF2 Jun 22 '12 at 05:07
  • I'll try it shortly, any idea how to do this in c++ ? – Thalia Jun 22 '12 at 05:09
  • 1
    For program files: TCHAR pf[MAX_PATH]; SHGetSpecialFolderPath( 0, pf, CSIDL_PROGRAM_FILES, FALSE ); – KF2 Jun 22 '12 at 05:13
  • 2
    These 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 – KF2 Jun 22 '12 at 05:24
1

You can get it using following,

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Rajesh Subramanian
  • 6,400
  • 5
  • 29
  • 42