Find out battery charge capacity in percentage using C# or .NET

后端 未结 9 1493
谎友^
谎友^ 2020-12-03 03:21

I have an application that gets detailed system information, and I have been able to get the percent of charge remaining but not the percent of the battery itself.

E

相关标签:
9条回答
  • 2020-12-03 03:35
    BatteryChargeStatus.Text = SystemInformation.PowerStatus.BatteryChargeStatus.ToString(); 
    BatteryFullLifetime.Text = SystemInformation.PowerStatus.BatteryFullLifetime.ToString();    
    BatteryLifePercent.Text = SystemInformation.PowerStatus.BatteryLifePercent.ToString(); 
    BatteryLifeRemaining.Text = SystemInformation.PowerStatus.BatteryLifeRemaining.ToString(); 
    PowerLineStatus.Text = SystemInformation.PowerStatus.PowerLineStatus.ToString();
    

    If you want to perform some operation just convert these string values into the integer.

    0 讨论(0)
  • 2020-12-03 03:37

    Simple code to get Battery Level in C#

    protected void batteryLevel ()
    {
        var filter  = new IntentFilter(Intent.ActionBatteryChanged);
        var battery = RegisterReceiver(null, filter);
        int level   = battery.GetIntExtra(BatteryManager.ExtraLevel, -1);
        int scale   = battery.GetIntExtra(BatteryManager.ExtraScale, -1);
    
        double level_0_to_100 = Math.Floor (level * 100D / scale);
    
    } 
    
    0 讨论(0)
  • 2020-12-03 03:38

    WMI worked for me (tested on 3 notebooks of different brands), but I had to use something like this:

    new ManagementObjectSearcher(@"\\localhost\root\wmi", 
            "Select FullChargedCapacity From BatteryFullChargedCapacity");
    // use value of resultingInstance.Properties["FullChargedCapacity"]
    
    new ManagementObjectSearcher(@"\\localhost\root\wmi",
            "Select DesignedCapacity From BatteryStaticData");
    //use value of resultingInstance2.Properties["DesignedCapacity"]
    
    0 讨论(0)
  • 2020-12-03 03:43

    No need to unnecessary complicate things. Try something like:

    using System.Management;
    
    PowerStatus pwr = SystemInformation.PowerStatus;
    
    String strBatteryChargingStatus;
    strBatteryChargingStatus = pwr.BatteryChargeStatus.ToString();
    MessageBox.Show("battery charge status : " + batterystatus);
    
    String strBatterylife;
    strBatterylife = pwr.BatteryLifePercent.ToString();
    MessageBox.Show("Battery life: "+batterylife);
    

    In this way you can get all of the battery information.

    0 讨论(0)
  • 2020-12-03 03:43
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace batterie
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                showbattrie();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            public void showbattrie()
            {
                PowerStatus status = SystemInformation.PowerStatus;
                textBox1.Text = status.BatteryLifePercent.ToString("P0");
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 03:45

    You can use the System.Windows.Forms.PowerStatus class - http://msdn.microsoft.com/en-us/library/system.windows.forms.powerstatus.aspx

    0 讨论(0)
提交回复
热议问题