VB.NET detect laptop or desktop
Using VB.NET detect laptop or desktop
You may run into a situation where you want to check the machine type and determine if your application is running on a desktop or laptop.
This article will discuss how to use VB.NET to detect laptop or desktop device type.
I should note, that there are a lot of ways to accomplish this.
You could:
- Utilize WMI classes, and then determine chassis type.
- Build a static array of models types in your environment, and then get the model type the program is running on
- Look at the power settings of the device (laptops typically have different power configurations than desktops)
None of these are going to work 100% of the time and are prone to error.
The method I ended up with is also not 100% foolproof.
The basic concept is that we’ll look to the battery to make a determination. Desktops don’t typically have batteries, right? Well, that’s true most of the time. For example, if you have a modern UPS hooked to a desktop via usb, chances are good the desktop will report as having an active battery.
That wasn’t going to pose an issue in my case, and I found the below code to work perfectly on accurately determining whether or not I was dealing with a laptop or desktop.
Determine if machine type is laptop or desktop:
'################################################################################ 'checks to see if device is a laptop or desktop and performs action accordingly Public Sub checkDeviceType() Try 'easiest way to to this is to check for a battery 'so, lets check the battery power status Dim ps As PowerStatus = SystemInformation.PowerStatus Dim batteryStatus As String = ps.BatteryChargeStatus.ToString 'the status for no batter is NoSystemBattery If batteryStatus = "NoSystemBattery" Then 'if no battery, than we are likely dealing with a desktop '*****Perform****** '*****Destkop****** '******Code******** Else 'we likely have a laptop '*****Perform****** '*****Laptop****** '******Code******** End If Catch ex As Exception 'Error control End Try End Sub '################################################################################
Have an even better solution? Feel free to post in the comments below.