[C#/WPF] ノートPCのバッテリーの充電状況などを取得する

# もくじ

https://tera1707.com/entry/2022/02/06/144447

# やりたいこと

C#アプリの中で、バッテリの充電が今どれくらいなのか？を知りたい。

# やり方

WinFormのAPIを使うやり方と、UWPのAPIを使うやり方があるっぽい。

### UWPのAPI版

`Windows.Devices.Power 名前空間`のクラスを使う。
WPFで使うには、下記の手順が必要。

- UWPのAPIを使えるように参照を追加([こちら](https://qiita.com/tera1707/private/981829d2bfdd157e3d19#%E3%82%84%E3%82%8A%E6%96%B9%EF%BC%91%E3%81%93%E3%82%8C%E3%81%A7%E5%8B%95%E3%81%84%E3%81%9F)参照)
- `using Windows.Devices.Power;`を追加


``` UWP版.cs
using System;
using Windows.Devices.Power;
using System.Threading;

// C:\Program Files (x86)\Windows Kits\10\UnionMetadata\Windows.winmd を参照に追加必要
namespace ConsoleApp49
{
    class Program
    {
        static Battery AggBattery = Battery.AggregateBattery;

        static void Main(string[] args)
        {
            // 変化したときに読んでもらう
            AggBattery.ReportUpdated += AggBattery_ReportUpdated;
            // 定期的にも表示する
            var timer = new Timer(((param) => { AggBattery_ReportUpdated(null, null); }), null, 0, 1000);

            Console.ReadLine();
        }

        private static void AggBattery_ReportUpdated(Battery sender, object args)
        {
            var report = Battery.AggregateBattery.GetReport();

            var Maximum = Convert.ToDouble(report.FullChargeCapacityInMilliwattHours);
            var Value = Convert.ToDouble(report.RemainingCapacityInMilliwattHours);
            var Percent = (Value / Maximum) * 100;

            var ValToFull = Maximum - Value;
            var TimeToFull = (ValToFull / report.ChargeRateInMilliwatts) * 60.0;
            var TimeToEmpty = (Value / report.ChargeRateInMilliwatts) * 60.0;

            if (sender != null)
                Console.Write("*");// 変化検出時に呼ばれた場合は*つける

            Console.Write("St:" + report.Status + " Charge:" + Percent.ToString("F2") + "% Charge Rate:" + report.ChargeRateInMilliwatts);
            Console.Write(" DesignCapacityInMilliwattHours:" + report.DesignCapacityInMilliwattHours + " FullChargeCapacityInMilliwattHours = " + report.FullChargeCapacityInMilliwattHours + "  RemainingCapacityInMilliwattHours:" + report.RemainingCapacityInMilliwattHours);

            if (report.ChargeRateInMilliwatts == 0)
            {
                // ChargeRateInMilliwattsが0ならACアダプタ挿抜直後等
                Console.WriteLine(" Calcurating...");
            }
            else if (report.ChargeRateInMilliwatts > 0)
            {
                // ChargeRateInMilliwattsが＋なら充電中(AC)
                Console.WriteLine(" TimeToFull = " + TimeToFull + " min");
            }
            else
            {
                // ChargeRateInMilliwattsが－なら放電中(SC)
                Console.WriteLine(" TimeToEmpty = " + (-TimeToEmpty) + " min");
            }

        }
    }
}

```

### WinForm版


`System.Windows.Forms名前空間`の`SystemInformation.PowerStatus`を使う。

``` WinForm版.cs
        static void Main(string[] args)
        {
            while (true)
            {
                var chargeStatus = SystemInformation.PowerStatus.BatteryChargeStatus;
                var fullLifetime = SystemInformation.PowerStatus.BatteryFullLifetime;
                var percent = SystemInformation.PowerStatus.BatteryLifePercent;
                var lifeRemaining = SystemInformation.PowerStatus.BatteryLifeRemaining;
                var powerlineStatus = SystemInformation.PowerStatus.PowerLineStatus;

                Console.WriteLine("chargeStatus = " + chargeStatus + "  fullLifetime = " + fullLifetime + "  percent = " + percent + "  lifeRemaining = " + lifeRemaining + "  powerlineStatus = " + powerlineStatus);

                Thread.Sleep(1000);
            }
        }
```



## 参考
### UWP版

`Battery`クラス(バッテリーの情報を扱う)
https://docs.microsoft.com/en-us/uwp/api/windows.devices.power.battery?view=winrt-19041
`BatteryReport`クラス(バッテリーの状況を入れとくためのクラス)
https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Power.BatteryReport?redirectedfrom=MSDN&view=winrt-19041

### Form版

SystemInformation クラス
https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.systeminformation?view=net-5.0

SystemInformation.PowerStatus
https://docs.microsoft.com/ja-jp/dotnet/api/system.windows.forms.systeminformation.powerstatus?view=net-5.0

※SystemInformation クラスには、システムの設定とかもろもろの情報が何でもかんでもつまってるっぽい。