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

前端 未结 29 2723
野性不改
野性不改 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:20

    All fine, but this should also work from env:

    PROCESSOR_ARCHITECTURE=x86
    

    ..

    PROCESSOR_ARCHITECTURE=AMD64
    

    Too easy, maybe ;-)

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 05:21

    Enjoy ;-)

    Function Is64Bit() As Boolean
    
        Return My.Computer.FileSystem.SpecialDirectories.ProgramFiles.Contains("Program Files (x86)")
    
    End Function
    
    0 讨论(0)
  • 2020-11-22 05:22

    .NET 4 has two new properties in the Environment class, Is64BitProcess and Is64BitOperatingSystem. Interestingly, if you use Reflector you can see they are implemented differently in the 32-bit & 64-bit versions of mscorlib. The 32-bit version returns false for Is64BitProcess and calls IsWow64Process via P/Invoke for Is64BitOperatingSystem. The 64-bit version just returns true for both.

    0 讨论(0)
  • 2020-11-22 05:22

    OSInfo.Bits

    using System;
    namespace CSharp411
    {
        class Program
        {
            static void Main( string[] args )
            {
               Console.WriteLine( "Operation System Information" );
               Console.WriteLine( "----------------------------" );
               Console.WriteLine( "Name = {0}", OSInfo.Name );
               Console.WriteLine( "Edition = {0}", OSInfo.Edition );
               Console.WriteLine( "Service Pack = {0}", OSInfo.ServicePack );
               Console.WriteLine( "Version = {0}", OSInfo.VersionString );
               Console.WriteLine( "Bits = {0}", OSInfo.Bits );
               Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:24

    Using dotPeek helps to see how the framework actually does it. With that in mind, here's what I've come up with:

    public static class EnvironmentHelper
    {
        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentProcess();
    
        [DllImport("kernel32.dll")]
        static extern IntPtr GetModuleHandle(string moduleName);
    
        [DllImport("kernel32")]
        static extern IntPtr GetProcAddress(IntPtr hModule, string procName);
    
        [DllImport("kernel32.dll")]
        static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);
    
        public static bool Is64BitOperatingSystem()
        {
            // Check if this process is natively an x64 process. If it is, it will only run on x64 environments, thus, the environment must be x64.
            if (IntPtr.Size == 8)
                return true;
            // Check if this process is an x86 process running on an x64 environment.
            IntPtr moduleHandle = GetModuleHandle("kernel32");
            if (moduleHandle != IntPtr.Zero)
            {
                IntPtr processAddress = GetProcAddress(moduleHandle, "IsWow64Process");
                if (processAddress != IntPtr.Zero)
                {
                    bool result;
                    if (IsWow64Process(GetCurrentProcess(), out result) && result)
                        return true;
                }
            }
            // The environment must be an x86 environment.
            return false;
        }
    }
    

    Example usage:

    EnvironmentHelper.Is64BitOperatingSystem();
    
    0 讨论(0)
提交回复
热议问题