如何在C#代码中获取操作系统详细信息

时间:2010-08-25 17:41:39

标签: c# wmi

我需要获得OsArchitecture意味着O / s的比特,我使用的是Win32_OperatingSystem,但它的“OsArchitecture”并不适用于所有类型的操作系统

ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
           ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
 foreach( ManagementObject mo in osDetailsCollection )
  {
    String  _operatingSysBits = mo["OSArchitecture"].ToString();

  }

2 个答案:

答案 0 :(得分:1)

检查IntPtr.Size。它在32位平台上为4,在64位平台上为8(除非您的进程在32位模式下运行)。

答案 1 :(得分:0)

对于.NET 4.0,有Environment.is64BitOperatingSystem

    ManagementObject Mo = new ManagementObject("Win32_Processor.DeviceID='CPU0'");
    object o = Mo["Architecture"];
    UInt16 sp = (UInt16)(o);

    if (sp == 0)
    {
        //86
    } else if (sp == 9)
    {
        //64
    }

    Mo.Dispose();