Directory
- 1. Use Windows form powerstatus
- 2. Use WMI
- 3. Use the new local API in Windows Vista
Returned directory
1. Use Windows form powerstatus
Advantages |
. Net Original Ecology |
Disadvantages |
No Event Notification |
The first method is to use the powerstatus attribute of systeminformation in Windows Forms to return a powerstatus object.
The batterychargestatus attribute can be used to obtain the batterychargestatus, and the power type also contains other attributes, such as batterylifepercent, to obtain detailed batterychargestatus information. However, the only drawback of using this method is that there is no event notification mechanism for changing the battery status, that is, developers need to take the initiative to call it.
Returned directory
2. Use WMI
Advantages |
The event notification capability is not perfect, and you do not need to reference the Windows form framework. |
Disadvantages |
System. Management. dll must be referenced because the Event Notification efficiency is not high. |
The other method is WMI, which queries the battery_status attribute of the win32_battery type. Similar to the following WQL statement:
Select batterystatus from win32_battery
Of course, if so, there is no difference with the first method.
Therefore, we can use WMI event queries so that when the battery status changes, we can get the information immediately.
For WMI, I will not talk about the theoretical content here. Readers can refer to other WMI articles on the Internet or my blog. Here, we directly go to the Code, through the "_ instancemodificationevent" WMI event type to specify the event notification after the object is modified, and then query the win32_battery type attributes. Code:
// Note: Reference System. Management. dll and using system. Management;
Static void main (string [] ARGs)
{
// Create a wql Event Query for instance Creation
VaR qcreate = new wqleventquery ("_ instancemodificationevent ",
Timespan. fromseconds (3), // whthin = 1
"Targetinstance ISA 'win32 _ battery '");
// Create a listener for Event Query (managementeventwatcher)
VaR wcreate = new managementeventwatcher (qcreate );
// Event registration code
Wcreate. eventarrived + = (sender, e) =>
{
Console. writeline ("Run: {0}", getinfo (E. newevent ));
};
// Start listening Asynchronously
Wcreate. Start ();
Console. writeline ("Stop monitoring by any key ");
System. Threading. thread. Sleep (-1 );
}
// Output information of managementbaseobject corresponding to the event (win32_battery instance in this example)
Static int getinfo (managementbaseobject mobj)
{
VaR instance = (managementbaseobject) mobj ["targetinstance"];
VaR prop = instance. properties ["batterystatus"];
Return (ushort) prop. value;
}
The program will directly output the status code of batterystatus. For more information, see the msdn documentation on win32_battery WMI. The running effect is as follows:
(When the above program runs, the same value will be continuously output, possibly because other properties of win32_battery have changed. Because the WMI Event Query instancemodificationevent is for one type, and we only output the batterystatus attribute)
But it seems that the win32_battery type is not the original WMI event type (refer to this article: http://blogs.msdn.com/ B /wmi/archive/2009/12/28/using-within-in-a-wmi-event-query.aspx ). In fact, the event notification is completed based on the within time value in the WQL Event Query. Obviously, the higher the within, the higher the accuracy, but the more resource consumption. Therefore, such event notifications are not perfect.
Returned directory
3. Use the new local API in Windows Vista
Advantages |
Event Notification capability |
Disadvantages |
For Windows Vista or a later version, you must call a local API or reference the Windows API code pack. |
The last method is the most perfect in terms of functionality. Of course, the price is for a system later than Windows Vista. You need to call a local API or reference the Windows API code pack. Since Windows Vista, Windows provides related APIs to notify users of changes in power status.
Here, we can reference the Windows API code pack to easily use C # To fulfill the above requirements. In the Microsoft. windowsapicodepack. applicationservices namespace, The powermanager type can be used to meet the requirements. The members of the powermanager type are as follows:
You can use the batterylifepercentchanged event to know the batterylifepercentchanged status. The getcurrentbatterystate method is used to obtain the battery information.
Of course, the powermanager type also provides other power battery-related functions. You can refer to the Windows API code pack documentation on your own.