using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Threading;
namespace JZPW1310_Battery
{
public class SYSTEM_POWER_STATUS_EX
{
public byte ACLineStatus;
public byte BatteryFlag;
public byte BatteryLifePercent;
public byte Reserved1;
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
public byte Reserved2;
public byte BackupBatteryFlag;
public byte BackupBatteryLifePercent;
public byte Reserved3;
public uint BackupBatteryLifeTime;
public uint BackupBatteryFullLifeTime;
}
public partial class MainForm : Form
{
[DllImport("coredll")]
private static extern uint GetSystemPowerStatusEx(SYSTEM_POWER_STATUS_EX lpSystemPowerStatus, bool fUpdate);
public static void Main(string[] args)
{
System.Threading.Timer t = new System.Threading.Timer(TimerCb, null, 0, 60000);
Console.ReadLine();
}
private static void TimerCb(Object o)
{
try {
SYSTEM_POWER_STATUS_EX status = new SYSTEM_POWER_STATUS_EX();
string dir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
if (GetSystemPowerStatusEx(status, false) == 1)
{
// Console.WriteLine("BatteryLifePercent=" + status.BatteryLifePercent.ToString());
StreamWriter sw = new StreamWriter(dir + @"\battery.status");
sw.WriteLine("[BatteryStatus]");
sw.WriteLine("BatteryLifePercent=" + status.BatteryLifePercent.ToString());
sw.Close();
GC.Collect();
}
} catch (
Exception ex) {
// Console.WriteLine(ex.Message + ' : ' + ex.StackTrace);
}
}
}
}