2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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

Last updated at Posted at 2020-11-12

もくじ

やりたいこと

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

やり方

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

UWPのAPI版

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

  • UWPのAPIを使えるように参照を追加(こちら参照)
  • 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 クラスには、システムの設定とかもろもろの情報が何でもかんでもつまってるっぽい。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?