如何确定哪个版本的Windows?

时间:2011-06-14 13:23:51

标签: c# .net-3.5 .net-2.0

  1. 如何确定哪个版本的Windows? WinXP,Vista或7等。
  2. 32或64位?
  3. UPD:for .Net 2.0 - 3.5

3 个答案:

答案 0 :(得分:21)

您正在寻找Environment.OSVersionEnvironment.Is64BitProcessEnvironment.Is64BitOperatingSystem属性。

在.Net 4.0之前,您可以通过检查IntPtr.Size是否为8来检查进程是否为64位,并且您可以检查操作系统是否为64-位使用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);

答案 1 :(得分:9)

查看Environment.OSVersionEnvironment.Is64BitOperatingSystem

答案 2 :(得分:1)

你可以做到

System.OperatingSystem osInfo = System.Environment.OSVersion;

查看this