Reputation: 4565
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;
}
Upvotes: 16
Views: 27465
Reputation: 58685
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.
Upvotes: 2
Reputation: 657
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:
if(SystemInformation.PowerStatus.BatteryChargeStatus ==BatteryChargeStatus.NoSystemBattery){
//desktop
}
else{
//laptop
}
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
Upvotes: 1
Reputation: 41
There is no need for both Other and Unknown check.
Change the condition to i > 1 && i < 25
and remember ChassisTypes
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.
Upvotes: 4
Reputation: 2060
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
Upvotes: 7
Reputation: 4565
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.
Upvotes: -3
Reputation: 2447
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
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.
Upvotes: 6
Reputation:
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?!
Upvotes: 2
Reputation: 118118
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:
This documentation is derived from the CIM class descriptions published by the DMTF.
Presumably, that means the manufacturer had to provide some information for Windows to be able to discover it.
Upvotes: 2