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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1013354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:32:38  来源:igfitidea点击:

How to check the machine type? laptop or desktop?

c#wmi

提问by Cooper.Wu

How to check current machine type? laptop or desktop ?

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

I got this from http://blog.csdn.net/antimatterworld/archive/2007/11/11/1878710.aspx,it works well on my home machine(Win2003 on laptop), it returns "Portable", but failed on my work machine(Vista on laptop), it returns "Other".

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

here is the code:

这是代码:


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;
}

采纳答案by Cooper.Wu

read registry key from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\pcmcia, the ‘Start' value, it's laptop if start =0, otherwise it's desktop machine if start doesn't exist or start != 0.

从 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\pcmcia 中读取注册表项,'Start' 值,如果 start = 0,则为笔记本电脑,如果 start 不存在或 start != 0,则为台式机。

回答by JosephStyons

I am pretty sure that this will depend on whether the manufacturer has bothered to provide that data on the current chipset. If they have not, then "Other" is the best you can do.

我很确定这将取决于制造商是否愿意提供有关当前芯片组的数据。如果他们没有,那么“其他”是你能做的最好的事情。

Check out this somewhat related article, which also suggests querying the BIOS directly.

查看这篇有点相关的文章,其中也建议直接查询 BIOS

回答by Sinan ünür

See http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspxand http://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspxwhich states that:

http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspxhttp://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspx这指出:

This documentation is derived from the CIM class descriptions published by the DMTF.

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

Presumably, that means the manufacturer had to provide some information for Windows to be able to discover it.

据推测,这意味着制造商必须为 Windows 提供一些信息才能发现它。

回答by Sinan ünür

I don't think there is a definitive right answer for this and I've found WMI unreliable for it but I have been using the Win32 function GetSystemPowerStatus() to determine if there is a system battery, obviously system battery == portable computer.

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

I've never tried this on a desktop with a UPS though?!

我从来没有在带有 UPS 的台式机上尝试过这个?!

回答by Kevin Newman

Here's a good Microsoft article that suggests looking at a few other WMI classes to get a better idea of whether the computer is a laptop or desktop:
http://technet.microsoft.com/en-us/library/cc180825.aspx

这是一篇很好的 Microsoft 文章,建议查看其他一些 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

And it also suggests to look in the registry for the Power scheme.

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

回答by HMcG

Well, I may be attempting to raise the dead here, but I would suggest that the most reliable method of determining a laptop would be the present of a lid status switch. See GetPwrCapabilitiesand System_power_Capabilities

好吧,我可能试图在这里复活死者,但我建议确定笔记本电脑的最可靠方法是存在盖子状态开关。请参阅GetPwrCapabilitiesSystem_power_Capabilities

回答by migjoe

There is no need for both Otherand Unknowncheck.

不需要其他未知检查。

Change the condition to i > 1 && i < 25and remember ChassisTypesis an array where OS returns what ever he thinks your system is.

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

It is possible to match more than single type. Your code only returns the first match.

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

回答by user1706618

In order to check if machine is laptop or desktop you can try to check battery status, using SystemPowerCapabilites.LidPresent or both of them.

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

Battery:

电池:

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

SystemPowerCapabilites.LidPresent:

SystemPowerCapabilites.LidPresent:

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

getSystemPowerCapabilites().LidPresent;

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

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