本文介绍了如何确定Windows的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 如何确定Windows的版本?的WinXP,Vista或Windows 7等。
  2. 在32位或64位?

UPD:对于.NET 2.0 - 3.5

UPD: for .Net 2.0 - 3.5

推荐答案

您正在寻找的 Environment.OSVersion Environment.Is64BitProcess Environment.Is64BitOperatingSystem 属性。

You're looking for the Environment.OSVersion, Environment.Is64BitProcess, and Environment.Is64BitOperatingSystem properties.

.NET 4.0中之前,您可以检查是否在进程是64位通过检查 IntPtr.Size 8 ,你可以检查操作系统是否是64位的使用this code :

Before .Net 4.0, you can check whether the process is 64-bit by checking whether IntPtr.Size is 8, and you can check whether the OS is 64-bit using this code:

public static bool Is64BitProcess
{
    get { return IntPtr.Size == 8; }
}

public static bool Is64BitOperatingSystem
{
    get
    {
        // Clearly if this is a 64-bit process we must be on a 64-bit OS.
        if (Is64BitProcess)
            return true;
        // Ok, so we are a 32-bit process, but is the OS 64-bit?
        // If we are running under Wow64 than the OS is 64-bit.
        bool isWow64;
        return ModuleContainsFunction("kernel32.dll", "IsWow64Process") && IsWow64Process(GetCurrentProcess(), out isWow64) && isWow64;
    }
}

static bool ModuleContainsFunction(string moduleName, string methodName)
{
    IntPtr hModule = GetModuleHandle(moduleName);
    if (hModule != IntPtr.Zero)
        return GetProcAddress(hModule, methodName) != IntPtr.Zero;
    return false;
}

[DllImport("kernel32.dll", SetLastError=true)]
[return:MarshalAs(UnmanagedType.Bool)]
extern static bool IsWow64Process(IntPtr hProcess, [MarshalAs(UnmanagedType.Bool)] out bool isWow64);
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError=true)]
extern static IntPtr GetCurrentProcess();
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
extern static IntPtr GetModuleHandle(string moduleName);
[DllImport("kernel32.dll", CharSet = CharSet.Ansi, SetLastError=true)]
extern static IntPtr GetProcAddress(IntPtr hModule, string methodName);

这篇关于如何确定Windows的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 16:22