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

前端 未结 29 2734
野性不改
野性不改 2020-11-22 04:53

In a .NET 2.0 C# application I use the following code to detect the operating system platform:

string os_platform = System.Environment.OSVersion.Platform.ToS         


        
29条回答
  •  灰色年华
    2020-11-22 05:21

    The full answer is this (taken from both stefan-mg, ripper234 and BobbyShaftoe's answer):

        [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);
    
        private bool Is64Bit()
        {
            if (IntPtr.Size == 8 || (IntPtr.Size == 4 && Is32BitProcessOn64BitProcessor()))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    
        private bool Is32BitProcessOn64BitProcessor()
        {
            bool retVal;
    
            IsWow64Process(Process.GetCurrentProcess().Handle, out retVal);
    
            return retVal;
        } 
    

    First check if you're in a 64 bit process. If you're not, check if the 32 bit process is a Wow64Process.

提交回复
热议问题