【问题标题】:Search both ProgramFiles and ProgramFiles(x86) using environment variables in C++ and C# / VB.NET [duplicate]使用 C++ 和 C#/VB.NET 中的环境变量搜索 ProgramFiles 和 ProgramFiles(x86) [重复]
【发布时间】:2012-06-24 09:50:48
【问题描述】:

可能重复:
C# - How to get Program Files (x86) on Windows Vista 64 bit

我正在尝试从我自己的第三方程序中启动。我在Program FilesProgram Files (x86) 中进行了快速搜索,我刚刚意识到getenv("ProgramFiles") 返回的路径实际上取决于我是在x64 还是Win32 中运行。

如何使用环境变量而不是硬编码名称搜索(在 C++ 和 C# 或 VB.NET 中)两个 Program Files 文件夹 - 因为无论我运行的程序版本如何用户机器,用户可能将另一台安装在不同的版本中?

我现在的代码:在 C++ 中:

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

在 VB.NET 中:

System.Environment.GetEnvironmentVariable("ProgramFiles")

我看了这个来源:http://msdn.microsoft.com/en-us/library/aa365743

但如果我按照他们说的去做,我就会一直得到 x86...

【问题讨论】:

    标签: c# c++ vb.net environment-variables wow64


    【解决方案1】:

    【讨论】:

    • 根据 System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) 在 64 位机器上返回“c:\Program Files”,除非代码是针对 x86 构建的,在这种情况下它返回“C:\Program Files (x86)”。
    • 我会尽快尝试,知道如何在 c++ 中执行此操作吗?
    • 对于程序文件:TCHAR pf[MAX_PATH]; SHGetSpecialFolderPath(0, pf, CSIDL_PROGRAM_FILES, FALSE);
    • 这两行将获得所需的目录: string prog32 = Environment.GetEnvironmentVariable("ProgramFiles");字符串 prog64 = Environment.GetEnvironmentVariable("ProgramW6432");结果:C:\Program Files (x86) 和 C:\Program Files
    【解决方案2】:

    您可以通过以下方式获得它,

    Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 1970-01-01
      • 2011-06-06
      • 2015-02-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多