[英]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
目录:
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.