473,413 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Detect power status of laptop

Hi

How do I detect whether my laptop is running on battery or using the
power outlet?

Thanks

Jacob Thastrup
Nov 21 '05 #1
4 4741
Jacob,
How do I detect whether my laptop is running on battery or using the
power outlet?


Try the GetSystemPowerStatus Win32 API function:

http://msdn.microsoft.com/library/en...owerstatus.asp

You can probably also get this information with WMI
(System.Management).

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 21 '05 #2
look at the Win32_PortableBattery class under WMI.
check the Availability, BatteryStatus and Status properties - those might be
what you're looking for.

hope this helps..
Imran.

"Jacob Thastrup.com" <Jacob@-RemoveMe-thastrup.com> wrote in message
news:OY**************@TK2MSFTNGP12.phx.gbl...
Hi

How do I detect whether my laptop is running on battery or using the
power outlet?

Thanks

Jacob Thastrup

Nov 21 '05 #3
* "Jacob Thastrup.com" <Jacob@-RemoveMe-thastrup.com> scripsit:
How do I detect whether my laptop is running on battery or using the
power outlet?


My FAQ:

Information about the system power status (battery status) can be obtained
using the 'GetSystemPowerStatus' platform function:

\\\
Imports System.IO
Imports System.Runtime.InteropServices
..
..
..
Private Declare Auto Function GetSystemPowerStatus Lib "kernel32.dll" ( _
ByRef lpSystemPowerStatus As SYSTEM_POWER_STATUS _
) As Boolean

<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEM_POWER_STATUS
Public ACLineStatus As ACLineStatus
Public BatteryFlag As BatteryFlag
Public BatteryLifePercent As Byte
Public Reserved1 As Byte
Public BatteryLifeTime As Int32
Public BatteryFullLifeTime As Int32
End Structure

Public Enum ACLineStatus As Byte
Offline = 0
Online = 1
UnknownStatus = 255
End Enum

<Flags()> _
Public Enum BatteryFlag As Byte
High = 1
Low = 2
Critical = 4
Charging = 8
NoSystemBattery = 128
UnknownStatus = 255
End Enum
..
..
..
Dim sps As SYSTEM_POWER_STATUS
If GetSystemPowerStatus(sps) Then
Dim sw As New StringWriter()
With sps
sw.WriteLine("ACLineStatus: " & .ACLineStatus.ToString())
sw.WriteLine("BatteryFlag: " & .BatteryFlag.ToString())
sw.WriteLine( _
"BatteryLifePercent: " & _
IIf( _
.BatteryLifePercent = 255, _
"Unknown", _
.BatteryLifePercent.ToString() & " percent" _
) _
)
sw.WriteLine( _
"BatteryLifeTime: " & _
IIf( _
.BatteryLifeTime = -1, _
"Unknown", _
.BatteryLifeTime.ToString() & " seconds" _
) _
)
sw.WriteLine( _
"BatteryFullLifeTime: " & _
IIf( _
.BatteryFullLifeTime = -1, _
"Unknown", _
.BatteryFullLifeTime.ToString() & " seconds" _
) _
)
End With
MsgBox(sw.ToString())
Else

' An error occured.
End If
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #4
Herfried,

I asssume this is no code directly written by you, IIF?

:-)

Cor
Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nick Nelson | last post by:
I am trying to write a windows service that will synchronize a SQL replica in the background. Every time the laptop is opened, the service catches the resumesuspend event, checks to see if a...
6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
4
by: Frank Meng | last post by:
Hi. I am trying a csharp sample from http://www.codeproject.com/csharp/socketsincs.asp . (Sorry I didn't post all the source codes here, please get the codes from above link if you want to try)....
13
by: Shailesh Humbad | last post by:
Here is an advanced PHP question. Can anyone think of a way to detect the number of bytes written to output when a script is aborted? I am sending a large file to the client, and I want to record...
2
by: Dan D | last post by:
I have a large install file (an exe) on my web server that people download and install from. Looking at my log files, I see a lot of people downloading it, but no way to tell for sure if they...
4
by: jim | last post by:
I need to know if a laptop is connected to the Bellsouth network or the Sprint wireless network. How can this be done with VB.Net 2005?
1
by: gordonjones | last post by:
I need to have my laptop not disconnect from the network when I close it but still lock itself (ctl+alt+del) If I use Suspend or Hibernate the network connection is lost. Having the screen saver...
2
by: yimma216 | last post by:
Powerpoint experienced a serious problem with the 'office special symbol input add-in' add-in. if you have seen this message multiple times, you should disable this add-in and check to see if an...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.