Determine if your Laptop is Running on battery using C# or VB.NET

Here’s some code to programmatically find out if your laptop is currently running on battery power. I have created a Windows Form application and added the following code on a button click event which uses the SystemInformation.PowerStatus property

C#

private void btnPower_Click(object sender, EventArgs e)
{
PowerLineStatus status = SystemInformation.PowerStatus.PowerLineStatus;
if (status == PowerLineStatus.Offline)
MessageBox.Show("Running on Battery");
else
MessageBox.Show("Running on Power");


}

VB.NET (Converted Code)

Private Sub btnPower_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim st As PowerLineStatus = SystemInformation.PowerStatus.PowerLineStatus
If st = PowerLineStatus.Offline Then
MessageBox.Show("Running on Battery")
Else
MessageBox.Show("Running on Power")
End If

End Sub

image






About The Author

Suprotim Agarwal
Suprotim Agarwal, Developer Technologies MVP (Microsoft Most Valuable Professional) is the founder and contributor for DevCurry, DotNetCurry and SQLServerCurry. He is the Chief Editor of a Developer Magazine called DNC Magazine. He has also authored two Books - 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal.

1 comment:

Unknown said...

How to find what device running a application?
Example
If i running a application in desktop or laptop or tablet or mobile devices