代码之家  ›  专栏  ›  技术社区  ›  Cooper.Wu

如何检查机器类型?笔记本电脑还是台式电脑?

  •  14
  • Cooper.Wu  · 技术社区  · 17 年前

    如何检查当前机器类型?笔记本电脑还是台式电脑?

    我从这里得到的 http://blog.csdn.net/antimatterworld/archive/2007/11/11/1878710.aspx ,它在我的家用电脑(Win2003在笔记本电脑上)上运行良好,它返回“便携”,但在我的工作电脑(Vista在笔记本电脑上)上失败,它返回“其他”。

    代码如下:

    
    public enum ChassisTypes
    {
        Other = 1,
        Unknown,
        Desktop,
        LowProfileDesktop,
        PizzaBox,
        MiniTower,
        Tower,
        Portable,
        Laptop,
        Notebook,
        Handheld,
        DockingStation,
        AllInOne,
        SubNotebook,
        SpaceSaving,
        LunchBox,
        MainSystemChassis,
        ExpansionChassis,
        SubChassis,
        BusExpansionChassis,
        PeripheralChassis,
        StorageChassis,
        RackMountChassis,
        SealedCasePC
    }
    
    public static ChassisTypes GetCurrentChassisType()
    {
        ManagementClass systemEnclosures = new ManagementClass("Win32_SystemEnclosure");
        foreach (ManagementObject obj in systemEnclosures.GetInstances())
        {
            foreach (int i in (UInt16[  ])(obj["ChassisTypes"]))
            {
                 if (i > 0 && i < 25)
                {
                    return (ChassisTypes)i;
                }
            }
        }
        return ChassisTypes.Unknown;
    }
    
    
    8 回复  |  直到 9 年前
        1
  •  7
  •   Kevin Newman    17 年前

    下面是一篇很好的微软文章,建议您查看其他几个WMI类,以便更好地了解计算机是笔记本电脑还是台式机:
    http://technet.microsoft.com/en-us/library/cc180825.aspx

    Win32_SystemEnclosure, ChassisTypes(1)=10
    Win32_Battery or Win32_PortableBattery
    Win32_PCMCIAController
    Win32_DriverVXD.Name = "pccard"
    Win32_ComputerSystem.Manufacturer
    Win32_ComputerSystem.Model
    

    它还建议在注册表中查找电源方案。

        2
  •  3
  •   HMcG    15 年前

    嗯,我可能想把死者抬到这里,但我建议确定笔记本电脑最可靠的方法是有一个盖子状态开关。 见 GetPwrCapabilities System_power_Capabilities

        3
  •  3
  •   VMAtm    13 年前

    两者都不需要 其他 未知 检查。

    将条件更改为 i > 1 && i < 25 并且记住 ChassisTypes 是一个数组,OS返回他认为您的系统是什么。

    可以匹配多个类型。您的代码只返回第一个匹配项。

        4
  •  2
  •   Sinan Ünür    17 年前

    http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspx http://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspx 其中规定:

    此文档源自DMTF发布的CIM类描述。

    大概,这意味着制造商必须提供一些信息让Windows能够发现它。

        5
  •  2
  •   user1333    17 年前

    我认为没有一个确定的正确答案,我发现wmi不可靠,但我一直在使用win32函数getSystemPowerStatus()来确定是否有系统电池,显然是系统电池==便携式计算机。

    我从来没有在带UPS的桌面上尝试过这个?!

        6
  •  2
  •   JosephStyons    10 年前

    我非常肯定这将取决于制造商是否费心提供当前芯片组的数据。如果他们没有,那么“其他”是你能做的最好的。

    Check out this somewhat related article 这也表明 querying the BIOS directly .

        7
  •  0
  •   user1706618    10 年前

    为了检查机器是笔记本电脑还是台式电脑,您可以尝试使用SystemPowerCapabilities.lidPresent或两者来检查电池状态。

    电池:

    if(SystemInformation.PowerStatus.BatteryChargeStatus ==BatteryChargeStatus.NoSystemBattery){
      //desktop 
    }
    else{
      //laptop
    }
    

    系统电源能力.lid存在:

    public SYSTEM_POWER_CAPABILITIES getSystemPowerCapabilites(){
    {
        SYSTEM_POWER_CAPABILITIES systemPowerCapabilites;
        GetPwrCapabilities(out systemPowerCapabilites);
        return systemPowerCapabilites;
    }
    
    getSystemPowerCapabilites().LidPresent;
    

    GetPwrCapabilities定义: http://www.pinvoke.net/default.aspx/powrprof/GetPwrCapabilities.html

        8
  •  -3
  •   Cooper.Wu    17 年前

    从hkey_local_machine\system\currentcontrolset\services\pcmcia中读取注册表项,start_值,如果start=0则为笔记本电脑,否则,如果start不存在或start,则为台式机!= 0。