如何查看机器类型? 笔记本电脑还是台式机?
如何查看当前机器类型? 笔记本电脑还是台式机?
我从 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;
}
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".
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
好吧,我可能试图在这里复活死者,但我建议确定笔记本电脑的最可靠方法是盖子状态开关的存在。
请参阅 GetPwrCapability 和 System_power_Capability
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 GetPwrCapabilities and System_power_Capabilities
这是一篇很好的 Microsoft 文章,建议查看其他一些 WMI 类,以更好地了解计算机是笔记本电脑还是台式机:
http://technet.microsoft.com/en-us/library/cc180825.aspx
它还建议在注册表中查找电源方案。
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
And it also suggests to look in the registry for the Power scheme.
无需同时进行其他和未知检查。
将条件更改为
i > 1 && 我< 25
并记住ChassisTypes
是一个数组,操作系统会返回他认为您的系统是什么的数组。可以匹配多个单一类型。 您的代码仅返回第一个匹配项。
There is no need for both Other and Unknown check.
Change the condition to
i > 1 && i < 25
and rememberChassisTypes
is an array where OS returns what ever he thinks your system is.It is possible to match more than single type. Your code only returns the first match.
请参阅 http://www.microsoft.com/technet/scriptcenter /resources/qanda/sept04/hey0921.mspx 和 http://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspx 其中指出:
据推测,这意味着制造商必须提供一些信息以便 Windows 能够发现它。
See http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept04/hey0921.mspx and http://msdn.microsoft.com/en-us/library/aa387204(VS.85).aspx which states that:
Presumably, that means the manufacturer had to provide some information for Windows to be able to discover it.
我认为对此没有明确的正确答案,并且我发现 WMI 对此不可靠,但我一直在使用 Win32 函数 GetSystemPowerStatus() 来确定是否有系统电池,显然系统电池==便携式计算机。
我从来没有在带有 UPS 的台式机上尝试过这个?!
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.
I've never tried this on a desktop with a UPS though?!
我非常确定这将取决于制造商是否愿意提供当前芯片组上的数据。 如果他们还没有,那么“其他”就是你能做的最好的事情。
查看这篇有些相关的文章,其中还建议< a href="http://www.codeguru.com/cpp/misc/misc/tools/article.php/c12347#Sample" rel="nofollow noreferrer">直接查询BIOS。
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.
为了检查机器是笔记本电脑还是台式机,您可以尝试使用 SystemPowerCapabilites.LidPresent 或两者都检查电池状态。
电池:
SystemPowerCapabilites.LidPresent:
GetPwrCapativity 定义:http://www.pinvoke.net/ default.aspx/powrprof/GetPwrCapability.html
In order to check if machine is laptop or desktop you can try to check battery status, using SystemPowerCapabilites.LidPresent or both of them.
Battery:
SystemPowerCapabilites.LidPresent:
GetPwrCapabilities definition: http://www.pinvoke.net/default.aspx/powrprof/GetPwrCapabilities.html
从 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\pcmcia 读取注册表项,“Start”值,如果 start =0 则为笔记本电脑,否则如果 start 不存在或 start != 0 则为台式机。
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.