查看并记录当前系统电池电量
原理:
用一个定时器每隔10秒就会读取系统的电池信息,然后显示到窗口中,并在后台写入到 log文件中.
效果:
主要代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace BatteryMeter
{
public partial class Form1 : Form
{
DateTime dt1, dt2;//定义全局变量
Boolean flag = false; //定义全局变量
public Form1()
{
InitializeComponent();
}
/*得到系统电池电量的信息
* 参考MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.powerstatus.aspx
*/
private void GetPowerStatus()
{
//此方法参考下面MSDN
//MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.powerstatus.aspx
Type t = typeof(System.Windows.Forms.PowerStatus);
PropertyInfo[] pi = t.GetProperties();
//将PowerStatus的5个属性名字添加到5个标签里
label1.Text = pi[0].Name;