C# - 如何在 Windows 64 位上获取程序文件 (x86)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/194157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:31:08  来源:igfitidea点击:

C# - How to get Program Files (x86) on Windows 64 bit

c#windowsfile64-bit

提问by Leonard H. Martin

I'm using:

我正在使用:

FileInfo(
    System.Environment.GetFolderPath(
        System.Environment.SpecialFolder.ProgramFiles) 
    + @"\MyInstalledApp"

In order to determine if a program is detected on a users machine (it's not ideal, but the program I'm looking for is a right old kludge of a MS-DOS application, and I couldn't think of another method).

为了确定是否在用户计算机上检测到程序(这并不理想,但我正在寻找的程序是 MS-DOS 应用程序的正确旧组合,我想不出另一种方法)。

On Windows XP and 32-bit versions of Windows Vista this works fine. However, on x64 Windows Vista the code returns the x64 Program Files folder, whereas the application is installed in Program Files x86. Is there a way to programatically return the path to Program Files x86 without hard wiring "C:\Program Files (x86)"?

在 Windows XP 和 32 位版本的 Windows Vista 上,这可以正常工作。但是,在 x64 Windows Vista 上,代码返回 x64 Program Files 文件夹,而应用程序安装在 Program Files x86 中。有没有办法以编程方式返回 Program Files x86 的路径而无需硬连线“C:\Program Files (x86)”?

采纳答案by JaredPar

The function below will return the x86 Program Filesdirectory in all of these three Windows configurations:

下面的函数将返回Program Files所有这三种 Windows 配置中的 x86目录:

  • 32 bit Windows
  • 32 bit program running on 64 bit Windows
  • 64 bit program running on 64 bit windows
  • 32位视窗
  • 在 64 位 Windows 上运行的 32 位程序
  • 在 64 位窗口上运行的 64 位程序

 

 

static string ProgramFilesx86()
{
    if( 8 == IntPtr.Size 
        || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
    {
        return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
    }

    return Environment.GetEnvironmentVariable("ProgramFiles");
}

回答by tomasr

One way would be to look for the "ProgramFiles(x86)" environment variable:

一种方法是查找“ProgramFiles(x86)”环境变量:

String x86folder = Environment.GetEnvironmentVariable("ProgramFiles(x86)");

回答by chadmyers

Note, however, that the ProgramFiles(x86)environment variable is only available if your application is running 64-bit.

但是请注意,ProgramFiles(x86)环境变量仅在您的应用程序运行 64 位时才可用。

If your application is running 32-bit, you can just use the ProgramFilesenvironment variable whose value will actually be "Program Files (x86)".

如果您的应用程序运行 32 位,您可以只使用ProgramFiles其值实际上是“Program Files (x86)”的环境变量。

回答by Carl H?rberg

Environment.GetEnvironmentVariable("PROGRAMFILES(X86)") ?? Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)

回答by Nathan

If you're using .NET 4, there is a special folder enumeration ProgramFilesX86:

如果您使用的是 .NET 4,则有一个特殊的文件夹枚举ProgramFilesX86

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

回答by Samir

I am writing an application which can run on both x86 and x64 platform for Windows 7 and querying the below variable just pulls the right program files folder path on any platform.

我正在编写一个可以在 Windows 7 的 x86 和 x64 平台上运行的应用程序,查询下面的变量只是在任何平台上拉出正确的程序文件文件夹路径。

Environment.GetEnvironmentVariable("PROGRAMFILES")

回答by Red John

One-liner using the new method in .NET. Will always return x86 Program Files folder.

使用 .NET 中的新方法进行单行。将始终返回 x86 Program Files 文件夹。

Environment.Is64BitOperatingSystem ? Environment.GetEnvironmentVariable("ProgramFiles(x86)") : Environment.GetEnvironmentVariable("ProgramFiles"))

Environment.Is64BitOperatingSystem ? Environment.GetEnvironmentVariable("ProgramFiles(x86)") : Environment.GetEnvironmentVariable("ProgramFiles"))

回答by Clint

C# Code:

C# 代码:

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86)

Output:

输出:

C:\Program Files (x86)

C:\程序文件 (x86)

Note:

笔记:

We need to tell the compiler to not prefer a particular build platform.

我们需要告诉编译器不要偏爱特定的构建平台。

Go to Visual Studio > Project Properties > Build > Uncheck "Prefer 32 bit"

Reason:

原因:

By default for most .NET Projects is "Any CPU 32-bit preferred"

When you uncheck 32 bit assembly will:

JIT to 32-bit code on 32 bit process

JIT to 32-bit code on 64 bit process

默认情况下,大多数 .NET 项目是“首选任何 CPU 32 位”

当您取消选中 32 位程序集时:

在 32 位进程上 JIT 到 32 位代码

在 64 位进程上 JIT 到 32 位代码