简体   繁体   English

在C#中,如何以编程方式了解操作系统是x64还是x86

[英]In C#, how can I know programmatically if the Operating system is x64 or x86

In C#, how can I know programmatically if the Operating system is x64 or x86 在C#中,如何以编程方式了解操作系统是x64还是x86

I found this API method on the internet, but it doesn't work 我在互联网上找到了这种API方法,但它不起作用

[DllImport("kernel32.dll")]
public static extern bool IsWow64Process(System.IntPtr hProcess, out bool lpSystemInfo);

public static bool IsWow64Process1
{
   get
   {
       bool retVal = false;
       IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, out retVal);
       return retVal;
   }
}

Thanks in advance. 提前致谢。

In .NET 4.0 you can use the new Environment.Is64BitOperatingSystem property. 在.NET 4.0中,您可以使用新的Environment.Is64BitOperatingSystem属性。

And this is how it's impemented 这就是它的成功

public static bool Is64BitOperatingSystem
{
    [SecuritySafeCritical]
    get
    {
        bool flag;
        return ((Win32Native.DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && Win32Native.IsWow64Process(Win32Native.GetCurrentProcess(), out flag)) && flag);
    }
}

Use reflector or similar to see exactly how it works. 使用反射器或类似物来确切​​了解它是如何工作的。

bool x86 = IntPtr.Size == 4;

If you build against AnyCPU, and you run on a 64-bit system, it will run on the 64-bit version of the framework. 如果您针对AnyCPU构建,并且您在64位系统上运行,它将在64位版本的框架上运行。 On a 32-bit system, it will run on the 32-bit version of the framework. 在32位系统上,它将在32位版本的框架上运行。 You can use this to its advantage by simply checking the IntPtr.Size property. 只需检查IntPtr.Size属性,就可以使用它。 If the Size = 4, you are running on 32-bit, Size = 8, you are running on 64-bit. 如果Size = 4,则运行在32位, Size = 8,您运行的是64位。

Have a look at this: 看看这个:

http://msdn.microsoft.com/en-us/library/system.environment_members.aspx http://msdn.microsoft.com/en-us/library/system.environment_members.aspx

I think you are looking for System.Environment.OSVersion 我认为您正在寻找System.Environment.OSVersion

The following is from this answer , so don't upvote me for it :) 以下是这个答案 ,所以不要赞成我:)

if (8 == IntPtr.Size
    || (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
{
    // x64
}
else
{
    // x86
}

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

相关问题 C# 中的 x86/x64 CPUID - x86/x64 CPUID in C# C# 支持 x64 和 x86 - C# support both x64 and x86 在x86系统上编译C#.NET x64二进制文件 - Compiling a C# .NET x64 binary on an x86 system 如何从 c# 中的 x86 应用程序启动 x64 位应用程序 - How to start x64 bit application from x86 application in c# 如何使用C#在x64或x86中运行PowerShell? - How to run PowerShell in x64 or x86 using C#? 如何在不使用宏的情况下使C#结构与x86和x64兼容? - how to make a C# struct to be compatible with both x86 and x64 without using macro? 安装我的应用程序时如何区分x86和x64操作系统 - How to differentiate between x86 and x64 Operating Systems when Install my application VisualStudio C#x64,为什么AddReference选项,.NET选项卡指向x86 DLL而不是x64? - VisualStudio C# x64, why AddReference option, .NET tab points to x86 DLL instead of x64? c#visual studio使用目标anycpu构建exe,但在调用进程平台(x86 / x64)上确定其平台(x86 / x64) - c# visual studio build exe with target anycpu but that determines its platform(x86/x64) on the calling process platform(x86/x64) 如何知道在构建中使用哪个cpu(x86 x64或AnyCpu)? - how to know which cpu to use in build (x86 x64 or AnyCpu)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM