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

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

    If you're using .NET Framework 4.0, it's easy:

    Environment.Is64BitOperatingSystem
    

    See Environment.Is64BitOperatingSystem Property (MSDN).

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

    @foobar: You are right, it is too easy ;)

    In 99% of the cases, developers with weak system administrator backgrounds ultimately fail to realize the power Microsoft has always provided for anyone to enumerate Windows.

    System administrators will always write better and simpler code when it comes to such a point.

    Nevertheless, one thing to note, build configuration must be AnyCPU for this environment variable to return the correct values on the correct systems:

    System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE")
    

    This will return "X86" on 32-bit Windows, and "AMD64" on 64-bit Windows.

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

    Quickest way:

    if(IntPtr.Size == 8) {
        // 64 bit machine
    } else if(IntPtr.Size == 4)  {
        // 32 bit machine
    } 
    

    Note: this is very direct and works correctly on 64-bit only if the program does not force execution as a 32-bit process (e.g. through <Prefer32Bit>true</Prefer32Bit> in the project settings).

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

    This is a solution based on Microsoft's code at http://1code.codeplex.com/SourceControl/changeset/view/39074#842775. It uses extension methods for easy code reuse.

    Some possible usage is shown below:

    bool bIs64BitOS = System.Environment.OSVersion.IsWin64BitOS();
    
    bool bIs64BitProc = System.Diagnostics.Process.GetCurrentProcess().Is64BitProc();
    
    //Hosts the extension methods  
    public static class OSHelperTools  
    {  
        /// <summary>     
        /// The function determines whether the current operating system is a      
        /// 64-bit operating system.     
        /// </summary>     
        /// <returns>     
        /// The function returns true if the operating system is 64-bit;      
        /// otherwise, it returns false.     
        /// </returns>    
        public static bool IsWin64BitOS(this OperatingSystem os)  
        {  
            if (IntPtr.Size == 8)  
            // 64-bit programs run only on Win64           
                return true;   
            else// 32-bit programs run on both 32-bit and 64-bit Windows     
            {   // Detect whether the current process is a 32-bit process                
                // running on a 64-bit system.               
                return Process.GetCurrentProcess().Is64BitProc();  
            }  
        }  
    
        /// <summary>  
        /// Checks if the process is 64 bit  
        /// </summary>  
        /// <param name="os"></param>  
        /// <returns>  
        /// The function returns true if the process is 64-bit;        
        /// otherwise, it returns false.  
        /// </returns>    
        public static bool Is64BitProc(this System.Diagnostics.Process p)  
        {  
            // 32-bit programs run on both 32-bit and 64-bit Windows           
            // Detect whether the current process is a 32-bit process                
            // running on a 64-bit system.               
            bool result;  
            return ((DoesWin32MethodExist("kernel32.dll", "IsWow64Process") && IsWow64Process(p.Handle, out result)) && result);  
        }  
    
        /// <summary>     
        /// The function determins whether a method exists in the export      
        /// table of a certain module.     
        /// </summary>     
        /// <param name="moduleName">The name of the module</param>     
        /// <param name="methodName">The name of the method</param>     
        /// <returns>     
        /// The function returns true if the method specified by methodName      
        /// exists in the export table of the module specified by moduleName.     
        /// </returns>       
        static bool DoesWin32MethodExist(string moduleName, string methodName)  
        {  
            IntPtr moduleHandle = GetModuleHandle(moduleName);  
            if (moduleHandle == IntPtr.Zero)  
                return false;    
            return (GetProcAddress(moduleHandle, methodName) != IntPtr.Zero);   
        }  
        [DllImport("kernel32.dll")]  
        static extern IntPtr GetCurrentProcess();  
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]  
        static extern IntPtr GetModuleHandle(string moduleName);  
    
        [DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]  
        static extern IntPtr GetProcAddress(IntPtr hModule, [MarshalAs(UnmanagedType.LPStr)]string procName);  
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]  
        [return: MarshalAs(UnmanagedType.Bool)]  
        static extern bool IsWow64Process(IntPtr hProcess, out bool wow64Process);  
    }
    
    0 讨论(0)
  • 2020-11-22 05:33

    I found this to be the best way to check for the platform of the system and the process:

    bool 64BitSystem = Environment.Is64BitOperatingSystem;
    bool 64BitProcess = Environment.Is64BitProcess;
    

    The first property returns true for 64-bit system, and false for 32-bit. The second property returns true for 64-bit process, and false for 32-bit.

    The need for these two properties is because you can run 32-bit processes on 64-bit system, so you will need to check for both the system and the process.

    0 讨论(0)
提交回复
热议问题