Search code examples
c#winformswinapiperfmon

Battery Status in winforms


I have been creating a winform application using perfmon. I have found out that the battery status will not work because its part of windows management. So I decide to go the wmi route.

So my question is when I put the battery status in a label as shown below:

private void BatteryStatus()
    {
        System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
        var allBatteries = wmi.GetInstances();

        foreach (var battery in allBatteries)
        {
            int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]);
            if (estimatedChargeRemaining == 100)
            {
                label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "%";
            }
        }



    }

The charge remaining is shown perfectly. My question is, is there a way that I can have just one if statement to call the battery status from 100 to 1

or the way I am doing it will I have to do 99 more if statements?

this is part of my performance monitor I am custom building. It would be easier if perfmon would allow the counter. This is the only way I can think of to get the battery stats such as:

Charge Rate
Discharge Rate
Remaining Capacity
Voltage

I have always did if statements with the labels on battery status. Before I go into doing 99 more if statements I want to be sure there is not an easier way?

*********** Update ************ I figured it out. Thanks for the help for the ones who helped.


Solution

  • I thing that what you want to do is this:

    private void BatteryStatus()
    {
        System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
        var allBatteries = wmi.GetInstances();
    
        foreach (var battery in allBatteries)
        {
            int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]);           
            label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "%";
        }
    }
    

    No need for and if statment, the label will be updated no matter what the percentage is.

    On the second part of the question you say that you want to show the "battery status", you can then use if like this:

      private void BatteryStatus()
     {
        System.Management.ManagementClass wmi  = new System.Management.ManagementClass("Win32_Battery");
        var allBatteries = wmi.GetInstances();
    
        foreach (var battery in allBatteries)
        {
            int estimatedChargeRemaining = Convert.ToInt32(battery["EstimatedChargeRemaining"]); 
            string Status = "";    
            if(estimatedChargeRemaining < 15) Status = "Critical";
            else  if(estimatedChargeRemaining < 35) Status = "Low";
            else  if(estimatedChargeRemaining < 60) Status = "Regular";
            else  if(estimatedChargeRemaining < 90) Status = "High";
            else Status = "Complete";
    
            label13.Text = "Remaining:" + "  " + estimatedChargeRemaining + "  " + "% | Status: " + Status;
        }
    }