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

16,259

Solution 1

Take a look at these Stack Overflow questions:

Solution 2

You can get it using following,

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
Share:
16,259
Thalia
Author by

Thalia

Junior developer exploring image processing and driver development

Updated on June 04, 2022

Comments

  • Thalia
    Thalia almost 2 years

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

  • KF2
    KF2 almost 12 years
    according System.Environment.GetFolderPath(System.Environment.SpecialF‌​older.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
    Thalia almost 12 years
    I'll try it shortly, any idea how to do this in c++ ?
  • KF2
    KF2 almost 12 years
    For program files: TCHAR pf[MAX_PATH]; SHGetSpecialFolderPath( 0, pf, CSIDL_PROGRAM_FILES, FALSE );
  • KF2
    KF2 almost 12 years
    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