Display system battery status using Management namespace in ASP.Net using C# and VB.Net

mahesh213
 
on Jun 28, 2021 10:52 PM
Sample_998938.zip
620 Views

Hi,

Display system battery status using system.management namespace

Display system battery status using C# and VB.Net in ASP.Net

in above url you have used 

using System.Windows.Forms;

but i need to use 

using System.Management;

 

SelectQuery Sq = new SelectQuery("Win32_Battery");
ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
ManagementObjectCollection osDetailsCollection = objOSDetails.Get();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 29, 2021 05:35 AM

Hi mahesh213,

Check this example. Now please take its reference and correct your code.

Namespaces

C#

using System.Management;
using System.Text;

VB.Net

Imports System.Management
Imports System.Text

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    SelectQuery Sq = new SelectQuery("Win32_Battery");
    ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
    ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
    StringBuilder sb1 = new StringBuilder();
    foreach (ManagementObject mo in osDetailsCollection)
    {
        foreach (PropertyData property in mo.Properties)
        {
            sb1.AppendLine(string.Format("{0} : {1}", property.Name, Convert.ToSingle(property.Value)));
        }
    }

    // OR
    StringBuilder sb = new StringBuilder();
    foreach (ManagementObject mo in osDetailsCollection)
    {
        sb.AppendLine(string.Format("Availability: {0}", (ushort)mo["Availability"]));
        sb.AppendLine(string.Format("BatteryRechargeTime: {0}", (string)mo["BatteryRechargeTime"]));
        sb.AppendLine(string.Format("BatteryStatus: {0}", (ushort)mo["BatteryStatus"]));
        sb.AppendLine(string.Format("Caption: {0}", (string)mo["Caption"]));
        sb.AppendLine(string.Format("Chemistry: {0}", (ushort)mo["Chemistry"]));
        sb.AppendLine(string.Format("ConfigManagerErrorCode: {0}", (string)mo["ConfigManagerErrorCode"]));
        sb.AppendLine(string.Format("InstallDate: {0}", Convert.ToDateTime(mo["InstallDate"]).ToString()));
        sb.AppendLine(string.Format("ConfigManagerUserConfig: {0}", (string)mo["ConfigManagerUserConfig"]));
        sb.AppendLine(string.Format("CreationClassName : {0}", (string)mo["CreationClassName"]));
        sb.AppendLine(string.Format("Description: {0}", (string)mo["Description"]));
        sb.AppendLine(string.Format("DesignCapacity : {0}", (string)mo["DesignCapacity"]));
        sb.AppendLine(string.Format("DesignVoltage: {0}", (ulong)mo["DesignVoltage"]));
        sb.AppendLine(string.Format("DeviceID : {0}", (string)mo["DeviceID"]));
        sb.AppendLine(string.Format("ErrorCleared: {0}", (string)mo["ErrorCleared"]));
        sb.AppendLine(string.Format("ErrorDescription : {0}", (string)mo["ErrorDescription"]));
        sb.AppendLine(string.Format("EstimatedChargeRemaining: {0}", (ushort)mo["EstimatedChargeRemaining"]));
        sb.AppendLine(string.Format("EstimatedRunTime : {0}", mo["EstimatedRunTime"]).ToString());
        sb.AppendLine(string.Format("ExpectedBatteryLife: {0}", (string)mo["ExpectedBatteryLife"]));
        sb.AppendLine(string.Format("ExpectedLife : {0}", (string)mo["ExpectedLife"]));
        sb.AppendLine(string.Format("FullChargeCapacity: {0}", (string)mo["FullChargeCapacity"]));
        sb.AppendLine(string.Format("LastErrorCode : {0}", (string)mo["LastErrorCode"]));
        sb.AppendLine(string.Format("MaxRechargeTime: {0}", (string)mo["MaxRechargeTime"]));
        sb.AppendLine(string.Format("Name : {0}", (string)mo["Name"]));
        sb.AppendLine(string.Format("PNPDeviceID: {0}", (string)mo["PNPDeviceID"]));
        sb.AppendLine(string.Format("PowerManagementSupported : {0}", mo["PowerManagementSupported"]).ToString());
        sb.AppendLine(string.Format("SmartBatteryVersion: {0}", (string)mo["SmartBatteryVersion"]));
        sb.AppendLine(string.Format("Status : {0}", (string)mo["Status"]));
        sb.AppendLine(string.Format("SystemCreationClassName : {0}", (string)mo["SystemCreationClassName"]));
        sb.AppendLine(string.Format("SystemName: {0}", (string)mo["SystemName"]));
        sb.AppendLine(string.Format("TimeToFullCharge : {0}", (string)mo["TimeToFullCharge"]));
        sb.AppendLine(string.Format("TimeOnBattery: {0}", (string)mo["TimeOnBattery"]));
        UInt16[] PowerManagement = (UInt16[])mo["PowerManagementCapabilities"];
        foreach (uint version in PowerManagement)
        {
            sb.AppendLine(string.Format("PowerManagementCapabilities: {0}", version.ToString()));
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim Sq As SelectQuery = New SelectQuery("Win32_Battery")
    Dim objOSDetails As ManagementObjectSearcher = New ManagementObjectSearcher(Sq)
    Dim osDetailsCollection As ManagementObjectCollection = objOSDetails.[Get]()
    Dim sb1 As StringBuilder = New StringBuilder()
    For Each mo As ManagementObject In osDetailsCollection
        For Each [property] As PropertyData In mo.Properties
            sb1.AppendLine(String.Format("{0} : {1}", [property].Name, Convert.ToSingle([property].Value)))
        Next
    Next
    ' OR
    Dim sb As StringBuilder = New StringBuilder()
    For Each mo As ManagementObject In osDetailsCollection
        sb.AppendLine(String.Format("Availability: {0}", CUShort(mo("Availability"))))
        sb.AppendLine(String.Format("BatteryRechargeTime: {0}", CStr(mo("BatteryRechargeTime"))))
        sb.AppendLine(String.Format("BatteryStatus: {0}", CUShort(mo("BatteryStatus"))))
        sb.AppendLine(String.Format("Caption: {0}", CStr(mo("Caption"))))
        sb.AppendLine(String.Format("Chemistry: {0}", CUShort(mo("Chemistry"))))
        sb.AppendLine(String.Format("ConfigManagerErrorCode: {0}", CStr(mo("ConfigManagerErrorCode"))))
        sb.AppendLine(String.Format("InstallDate: {0}", Convert.ToDateTime(mo("InstallDate")).ToString()))
        sb.AppendLine(String.Format("ConfigManagerUserConfig: {0}", CStr(mo("ConfigManagerUserConfig"))))
        sb.AppendLine(String.Format("CreationClassName : {0}", CStr(mo("CreationClassName"))))
        sb.AppendLine(String.Format("Description: {0}", CStr(mo("Description"))))
        sb.AppendLine(String.Format("DesignCapacity : {0}", CStr(mo("DesignCapacity"))))
        sb.AppendLine(String.Format("DesignVoltage: {0}", CULng(mo("DesignVoltage"))))
        sb.AppendLine(String.Format("DeviceID : {0}", CStr(mo("DeviceID"))))
        sb.AppendLine(String.Format("ErrorCleared: {0}", CStr(mo("ErrorCleared"))))
        sb.AppendLine(String.Format("ErrorDescription : {0}", CStr(mo("ErrorDescription"))))
        sb.AppendLine(String.Format("EstimatedChargeRemaining: {0}", CUShort(mo("EstimatedChargeRemaining"))))
        sb.AppendLine(String.Format("EstimatedRunTime : {0}", mo("EstimatedRunTime")).ToString())
        sb.AppendLine(String.Format("ExpectedBatteryLife: {0}", CStr(mo("ExpectedBatteryLife"))))
        sb.AppendLine(String.Format("ExpectedLife : {0}", CStr(mo("ExpectedLife"))))
        sb.AppendLine(String.Format("FullChargeCapacity: {0}", CStr(mo("FullChargeCapacity"))))
        sb.AppendLine(String.Format("LastErrorCode : {0}", CStr(mo("LastErrorCode"))))
        sb.AppendLine(String.Format("MaxRechargeTime: {0}", CStr(mo("MaxRechargeTime"))))
        sb.AppendLine(String.Format("Name : {0}", CStr(mo("Name"))))
        sb.AppendLine(String.Format("PNPDeviceID: {0}", CStr(mo("PNPDeviceID"))))
        sb.AppendLine(String.Format("PowerManagementSupported : {0}", mo("PowerManagementSupported")).ToString())
        sb.AppendLine(String.Format("SmartBatteryVersion: {0}", CStr(mo("SmartBatteryVersion"))))
        sb.AppendLine(String.Format("Status : {0}", CStr(mo("Status"))))
        sb.AppendLine(String.Format("SystemCreationClassName : {0}", CStr(mo("SystemCreationClassName"))))
        sb.AppendLine(String.Format("SystemName: {0}", CStr(mo("SystemName"))))
        sb.AppendLine(String.Format("TimeToFullCharge : {0}", CStr(mo("TimeToFullCharge"))))
        sb.AppendLine(String.Format("TimeOnBattery: {0}", CStr(mo("TimeOnBattery"))))
        Dim PowerManagement As UInt16() = CType(mo("PowerManagementCapabilities"), UInt16())

        For Each version As UInteger In PowerManagement
            sb.AppendLine(String.Format("PowerManagementCapabilities: {0}", version.ToString()))
        Next
    Next
End Sub