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

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

    From Chriz Yuen blog

    C# .Net 4.0 Introduced two new environment property Environment.Is64BitOperatingSystem; Environment.Is64BitProcess;

    Please be careful when you use these both property. Test on Windows 7 64bits Machine

    //Workspace: Target Platform x86
    Environment.Is64BitOperatingSystem True
    Environment.Is64BitProcess False
    
    //Workspace: Target Platform x64
    Environment.Is64BitOperatingSystem True
    Environment.Is64BitProcess True
    
    //Workspace: Target Platform Any
    Environment.Is64BitOperatingSystem True
    Environment.Is64BitProcess True
    
    0 讨论(0)
  • 2020-11-22 05:15

    I use:

    Dim drivelet As String = Application.StartupPath.ToString
    If Directory.Exists(drivelet(0) & ":\Program Files (x86)") Then
        MsgBox("64bit")
    Else
        MsgBox("32bit")
    End if
    

    This gets the path where your application is launched in case you have it installed in various places on the computer. Also, you could just do the general C:\ path since 99.9% of computers out there have Windows installed in C:\.

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

    Here's a Windows Management Instrumentation (WMI) approach:

    string _osVersion = "";
    string _osServicePack = "";
    string _osArchitecture = "";
    
    ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_OperatingSystem");
    ManagementObjectCollection collection = searcher.Get();
    
    foreach (ManagementObject mbo in collection)
    {
        _osVersion = mbo.GetPropertyValue("Caption").ToString();
        _osServicePack = string.Format("{0}.{1}", mbo.GetPropertyValue("ServicePackMajorVersion").ToString(), mbo.GetPropertyValue("ServicePackMinorVersion").ToString());
    
        try
        {
            _osArchitecture = mbo.GetPropertyValue("OSArchitecture").ToString();
        }
        catch
        {
            // OSArchitecture only supported on Windows 7/Windows Server 2008
        }
    }
    
    Console.WriteLine("osVersion     : " + _osVersion);
    Console.WriteLine("osServicePack : " + _osServicePack);
    Console.WriteLine("osArchitecture: " + _osArchitecture);
    
    /////////////////////////////////////////
    // Test on Windows 7 64-bit
    //
    // osVersion     : Microsoft Windows 7 Professional
    // osservicePack : 1.0
    // osArchitecture: 64-bit
    
    /////////////////////////////////////////
    // Test on Windows Server 2008 64-bit
    //    --The extra r's come from the registered trademark
    //
    // osVersion     : Microsoftr Windows Serverr 2008 Standard
    // osServicePack : 1.0
    // osArchitecture: 64-bit
    
    /////////////////////////////////////////
    // Test on Windows Server 2003 32-bit
    //    --OSArchitecture property not supported on W2K3
    //
    // osVersion     : Microsoft(R) Windows(R) Server 2003, Standard Edition
    // osServicePack : 2.0
    // osArchitecture:
    
    0 讨论(0)
  • 2020-11-22 05:17

    I use a version of the following:

        public static bool Is64BitSystem()
        {
            if (Directory.Exists(Environment.GetEnvironmentVariable("Program Files (x86)"))) return true;
            else return false;
        }
    
    0 讨论(0)
  • 2020-11-22 05:19

    Here is the direct approach in C# using DllImport from this page.

    [DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo); 
    
    public static bool Is64Bit() 
    { 
        bool retVal; 
    
        IsWow64Process(Process.GetCurrentProcess().Handle, out retVal); 
    
        return retVal; 
    } 
    
    0 讨论(0)
  • 2020-11-22 05:20

    Try this:

    Environment.Is64BitOperatingSystem
    
    Environment.Is64BitProcess
    
    0 讨论(0)
提交回复
热议问题