简体   繁体   English

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

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

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.在 Windows XP 和 32 位版本的 Windows Vista 上,这可以正常工作。 However, on x64 Windows Vista the code returns the x64 Program Files folder, whereas the application is installed in Program Files x86.但是,在 x64 Windows Vista 上,代码返回 x64 Program Files 文件夹,而应用程序安装在 Program Files x86 中。 Is there a way to programatically return the path to Program Files x86 without hard wiring "C:\\Program Files (x86)"?有没有办法以编程方式返回 Program Files x86 的路径而无需硬连线“C:\\Program Files (x86)”?

The function below will return the x86 Program Files directory in all of these three Windows configurations:下面的函数将返回所有这三种 Windows 配置中的 x86 Program Files目录:

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

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

    return Environment.GetEnvironmentVariable("ProgramFiles");
}

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

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

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 ProgramFiles environment variable whose value will actually be "Program Files (x86)".如果您的应用程序运行 32 位,您可以只使用ProgramFiles环境变量,其值实际上是“Program Files (x86)”。

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

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

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

Environment.GetEnvironmentVariable("PROGRAMFILES")

One-liner using the new method in .NET.使用 .NET 中的新方法进行单行。 Will always return x86 Program Files folder.将始终返回 x86 Program Files 文件夹。

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

C# Code: C# 代码:

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"默认情况下,大多数 .NET 项目是“首选任何 CPU 32 位”

When you uncheck 32 bit assembly will:当您取消选中 32 位程序集时:

JIT to 32-bit code on 32 bit process在 32 位进程上 JIT 到 32 位代码

JIT to 32-bit code on 64 bit process在 64 位进程上 JIT 到 32 位代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#Windows程序将文件写入“ sysWOW64”和“程序文件(x86)”。 使用VS安装项目。 64位系统上的32位应用程序 - C# Windows Program writes files to “sysWOW64” and to “Program Files(x86)”. Using VS Setup Projects. 32bit App on 64bit System 如何从 c# 中的 x86 应用程序启动 x64 位应用程序 - How to start x64 bit application from x86 application in c# 如何将文件复制到C:\\ Program Files(x86)\\ - How to copy file to the C:\Program Files (x86)\ 如果C#词典在x86上具有64位TKey,会发生什么情况? - What happens if a C# Dictionary has a 64-bit TKey on x86? 除非为 x86 平台构建,否则不安全的 c# 代码会导致 64 位平台上的堆损坏 - unsafe c# code causes heap corruption on 64 bit platform unless built for x86 platform 在 64 位机器上使用 C# 和“BUILD x86”访问注册表 - Registry access with C# and “BUILD x86” on a 64bit machine C#Visual Studio System.Windows.Media在程序文件(x86)\\参考程序集中不存在 - C# Visual Studio System.Windows.Media does not exist in Program Files (x86)\Referenced Assemblies 在项目设置为x86时,在C#中启动x64 Windows应用程序 - Launch x64 Windows application in C# while the project is set to x86 C# 中的 x86/x64 CPUID - x86/x64 CPUID in C# C# 支持 x64 和 x86 - C# support both x64 and x86
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM