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



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

sundar raj said...

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