Using C#, how to get whether my machine is 64bit or 32bit?

后端 未结 6 1030
陌清茗
陌清茗 2021-02-19 14:23

Using C#, I would like to create a method that retunrs whether my machine is 64 or 32-bit.

Is there anybody who knows how to do that?

6条回答
  •  盖世英雄少女心
    2021-02-19 14:56

    Here it is:

    How to detect Windows 64-bit platform with .NET?

    Quote:

    bool is64BitProcess = (IntPtr.Size == 8);
    bool is64BitOperatingSystem = is64BitProcess || InternalCheckIsWow64();
    
    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool IsWow64Process(
        [In] IntPtr hProcess,
        [Out] out bool wow64Process
    );
    
    public static bool InternalCheckIsWow64()
    {
        if ((Environment.OSVersion.Version.Major == 5 && Environment.OSVersion.Version.Minor >= 1) ||
            Environment.OSVersion.Version.Major >= 6)
        {
            using (Process p = Process.GetCurrentProcess())
            {
                bool retVal;
                if (!IsWow64Process(p.Handle, out retVal))
                {
                    return false;
                }
                return retVal;
            }
        }
        else
        {
            return false;
        }
    }
    

提交回复
热议问题