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

mahesh213
 
on Jun 22, 2021 05:33 AM
Sample_217576.zip
559 Views

Hi, 

I need to get below details using c# by using method

1) battery status (system battery percentage) 

2) full scale

3) line status 

http://csharphelper.com/blog/2016/01/display-battery-status-in-c/

I am getting confusion what namespace I have to include in my project

I am going to use same code

If you have different code can you please provide me

Can you please help me

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 23, 2021 12:44 AM

Hi mahesh213,

The PowerStatus belongs to System.Windows.Forms namespaces.

So you need to add the reference of System.Windows.Forms in your project.

Then inserit the System.Windows.Forms namespace to use it.

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

HTML

Charger Status:
<asp:TextBox runat="server" ID="txtChargeStatus" /><br />
Full Life:
<asp:TextBox runat="server" ID="txtFullLifetime" /><br />
Charge:
<asp:TextBox runat="server" ID="txtBatteryPercent" /><br />
Life Remaining:
<asp:TextBox runat="server" ID="txtLifeRemaining" /><br />
Line Status:
<asp:TextBox runat="server" ID="txtPowerLineStatus" />

Namespaces

C#

using System.Windows.Forms;

VB.Net

Imports System.Windows.Forms

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    PowerStatus status = SystemInformation.PowerStatus;
    txtChargeStatus.Text = status.BatteryChargeStatus.ToString();
    txtFullLifetime.Text = status.BatteryFullLifetime == -1 ? "Unknown" : status.BatteryFullLifetime.ToString();
    txtBatteryPercent.Text = status.BatteryLifePercent.ToString("P0");
    txtLifeRemaining.Text = status.BatteryLifeRemaining == -1 ? "Unknown" : status.BatteryLifeRemaining.ToString();
    txtPowerLineStatus.Text = status.PowerLineStatus.ToString();
}

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    Dim status As PowerStatus = SystemInformation.PowerStatus
    txtChargeStatus.Text = status.BatteryChargeStatus.ToString()
    txtFullLifetime.Text = If(status.BatteryFullLifetime = -1, "Unknown", status.BatteryFullLifetime.ToString())
    txtBatteryPercent.Text = status.BatteryLifePercent.ToString("P0")
    txtLifeRemaining.Text = If(status.BatteryLifeRemaining = -1, "Unknown", status.BatteryLifeRemaining.ToString())
    txtPowerLineStatus.Text = status.PowerLineStatus.ToString()
End Sub