C# - 如何在 Windows 64 位上获取程序文件 (x86) [英] C# - How to get Program Files (x86) on Windows 64 bit

查看:28
本文介绍了C# - 如何在 Windows 64 位上获取程序文件 (x86)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用:

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

为了确定是否在用户机器上检测到程序(这并不理想,但我正在寻找的程序是 MS-DOS 应用程序的正确老混杂,我想不出另一种方法).

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

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

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 配置中的 x86 Program Files 目录:

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

  • 32 位 Windows
  • 在 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");
}

这篇关于C# - 如何在 Windows 64 位上获取程序文件 (x86)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆