Subscribe For Free Updates!

We'll not spam mate! We promise.

Showing posts with label WMI Class Examples. Show all posts
Showing posts with label WMI Class Examples. Show all posts

Thursday, 17 October 2013

Get the Battery Details of Your System in Windows Form

Introduction

This article explains how to get the details of the
battery connected to your system. Here I will get the information from the Win32_Battery class.

What Win32_Battery is

The Win32_Battery WMI class a represents battery connected to the computer system..

Design

Create a new Windows Forms Application Project.

Add one button control to the form.

Design your screen as in the following screen:

1.jpeg

Next add a reference for "System.Management".
 


To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.

2.png

A window will open; in that choose the ".Net" tab.

3.jpg

It will show a list. In that list, choose "System.Management" and click the "OK" Button.
4.jpg
 Now go to code view
Add the namespace "using System.Management;".

Write the following code in the Button Click event
:

private
void button1_Click(object sender, EventArgs e)
{
        SelectQuery Sq = new SelectQuery("Win32_Battery");
        ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
        ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
        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()));
             }
        }
        MessageBox.Show(sb.ToString());
 }


In the code above I am getting the information from Win32_Battery and showing it in a Message Box on a button click.

SelectQuery

It represents a WQL (WMI Query Language) SELECT data query.

ManagementObjectSearcher

It retrieves a collection of management objects based on a specified query.

This class is one of the more commonly used entry points to retrieving management information.

ManagementObjectCollection

It represents various collections of management objects retrieved using WMI.

Now build your application. Click on the button; it will show the Battery details in a Message box.

5.jpeg

  Click here to download Source
Thank you.

Get the Details of Your Windows Operating System in ASP.Net

Introduction

This article describes how to get the details of your Windows Operating System. Here I will get the information from the Win32_OperatingSystem class.

Win32_OperatingSystem

The Win32_OperatingSystem WMI Class represents a Windows-based operating system installed on a computer. Any operating system that can be installed on a computer that can run a Windows-based operating system is a descendent or member of this class. Win32_OperatingSystem is a singleton class.
If a computer has multiple operating systems installed then this class only returns an instance for the currently active operating system. Design
Create a new ASP.Net Empty website.

Add one page in that website.

Write the following source code:


<
head runat="server">
    <title></title>
    <style type="text/css">
        html
        {
            font-size: 1.5em;
            font-family: Calibri;
            color: Red;
        }
    </style>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
    </div>
    </form>

</
body>
</
html>

Next add a reference for System.Management.

To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.

2.png

A window will open; in that choose the .Net tab.

3.jpeg

It will show a list. In that list, choose System.Management and click the "OK" button.



Now go to code view and write the following code:


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Management;
public
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SelectQuery Sq = new SelectQuery("Win32_OperatingSystem");
        ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
        ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
        foreach (ManagementObject mo in osDetailsCollection)
        {
            Response.Write("Name : " + mo["name"].ToString() + "<br/>");
            Response.Write("Version : " + mo["version"].ToString() + "<br/>");
            Response.Write("Csname : " + mo["csname"].ToString() + "<br/>");
            Response.Write("Boot Device : " + mo["BootDevice"].ToString() + "<br/>");
            Response.Write("Build Number : " + mo["BuildNumber"].ToString() + "<br/>");
            Response.Write("BuildType : " + mo["BuildType"].ToString() + "<br/>");
            Response.Write("Caption : " + mo["Caption"].ToString() + "<br/>");
            Response.Write("CodeSet : " + mo["CodeSet"].ToString() + "<br/>");
            Response.Write("CountryCode : " + mo["CountryCode"].ToString() + "<br/>");
            Response.Write("CreationClassName : " + mo["CreationClassName"].ToString() + "<br/>");
            Response.Write("CSCreationClassName : " + mo["CSCreationClassName"].ToString() + "<br/>");
            Response.Write("CSDVersion : " + mo["CSDVersion"].ToString() + "<br/>");
            Response.Write("CurrentTimeZone : " + mo["CurrentTimeZone"].ToString() + "<br/>");
            Response.Write("DataExecutionPrevention_Available : " + mo["DataExecutionPrevention_Available"].ToString() + "<br/>");
            Response.Write("DataExecutionPrevention_32BitApplications : " + mo["DataExecutionPrevention_32BitApplications"].ToString() +
"<br/>");
            Response.Write("DataExecutionPrevention_Drivers : " + mo["DataExecutionPrevention_Drivers"].ToString() + "<br/>");
            Response.Write("DataExecutionPrevention_SupportPolicy : " + mo["DataExecutionPrevention_SupportPolicy"].ToString() + "<br/>");
            Response.Write("Debug : " + mo["Debug"].ToString() + "<br/>");
            Response.Write("Description : " + mo["Description"].ToString() + "<br/>");
            Response.Write("Distributed : " + mo["Distributed"].ToString() + "<br/>");
            Response.Write("EncryptionLevel : " + mo["EncryptionLevel"].ToString() + "<br/>");
            Response.Write("ForegroundApplicationBoost : " + mo["ForegroundApplicationBoost"].ToString() + "<br/>");
            Response.Write("FreePhysicalMemory : " + mo["FreePhysicalMemory"].ToString() + "<br/>");
            Response.Write("FreeSpaceInPagingFiles : " + mo["FreeSpaceInPagingFiles"].ToString() + "<br/>");
            Response.Write("FreeVirtualMemory : " + mo["FreeVirtualMemory"].ToString() + "<br/>");
            Response.Write("InstallDate : " + mo["InstallDate"].ToString() + "<br/>");
            Response.Write("LastBootUpTime : " + mo["LastBootUpTime"].ToString() + "<br/>");
            Response.Write("LocalDateTime : " + mo["LocalDateTime"].ToString() + "<br/>");
            Response.Write("Locale : " + mo["Locale"].ToString() + "<br/>");
            Response.Write("Manufacturer : " + mo["Manufacturer"].ToString() + "<br/>");
            Response.Write("MaxNumberOfProcesses : " + mo["MaxNumberOfProcesses"].ToString() + "<br/>");
            Response.Write("MaxProcessMemorySize : " + mo["MaxProcessMemorySize"].ToString() + "<br/>");
            Response.Write("NumberOfProcesses : " + mo["NumberOfProcesses"].ToString() + "<br/>");                         
            Response.Write("NumberOfUsers : " + mo["NumberOfUsers"].ToString() + "<br/>");
            Response.Write("OperatingSystemSKU : " + mo["OperatingSystemSKU"].ToString() + "<br/>");
            Response.Write("Organization : " + mo["Organization"].ToString() + "<br/>");
            Response.Write("OSArchitecture : " + mo["OSArchitecture"].ToString() + "<br/>");
            Response.Write("OSLanguage : " + mo["OSLanguage"].ToString() + "<br/>");
            Response.Write("OSType : " + mo["OSType"].ToString() + "<br/>");
            Response.Write("PAEEnabled : " + mo["PAEEnabled"].ToString() + "<br/>");
            Response.Write("Primary : " + mo["Primary"].ToString() + "<br/>");
            Response.Write("ProductType : " + mo["ProductType"].ToString() + "<br/>");
            Response.Write("RegisteredUser : " + mo["RegisteredUser"].ToString() + "<br/>");
            Response.Write("SerialNumber : " + mo["SerialNumber"].ToString() + "<br/>");
            Response.Write("ServicePackMajorVersion : " + mo["ServicePackMajorVersion"].ToString() + "<br/>");
            Response.Write("ServicePackMinorVersion : " + mo["ServicePackMinorVersion"].ToString() + "<br/>");
            Response.Write("SizeStoredInPagingFiles : " + mo["SizeStoredInPagingFiles"].ToString() + "<br/>");
            Response.Write("Status : " + mo["Status"].ToString() + "<br/>");
            Response.Write("SuiteMask : " + mo["SuiteMask"].ToString() + "<br/>");
            Response.Write("SystemDevice : " + mo["SystemDevice"].ToString() + "<br/>");
            Response.Write("SystemDirectory : " + mo["SystemDirectory"].ToString() + "<br/>");
            Response.Write("SystemDrive : " + mo["SystemDrive"].ToString() + "<br/>");
            Response.Write("TotalVirtualMemorySize : " + mo["TotalVirtualMemorySize"].ToString() + "<br/>");
            Response.Write("TotalVisibleMemorySize : " + mo["TotalVisibleMemorySize"].ToString() + "<br/>");
            Response.Write("Version : " + mo["Version"].ToString() + "<br/>");
            Response.Write("WindowsDirectory : " + mo["WindowsDirectory"].ToString() + "<br/>");
        }
    }
}
In the code above I get the information from Win32_OperatingSystem and display that in the page.

SelectQueryIt represents a WMI Query Language (WQL) SELECT data query.

ManagementObjectSearcherIt retrieves a collection of management objects based on a specified query.

This class is one of the more commonly used entry points to retrieving management information.

ManagementObjectCollectionIt represents various collections of management objects retrieved using WMI.

Now build your application, it will show the details of your Windows OS in the page.



5.jpeg 

Click here to download source

Get All Intranet User Details in ASP.Net

Introduction

This article describes how to get the details of users present in an intranet. Here I will get the information from the Win32_UserAccount class.

What is Win32_UserAccount

The Win32_UserAccount WMI class contains information about a user account on a computer system running Windows.
 
Design
Create a new ASP.Net Empty website.

Add one page in that website.

In that page drag and drop a GridView control.

Design your screen as in the following screen.

1.png

Or you can copy the following source code:


<
body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" >
            <AlternatingRowStyle BackColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
        </asp:GridView>
    </div>
    </form>

</
body>

Next add a reference of System.Management.

To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.

2.png

A window will open; in that choose the .Net tab.

3.jpeg

It will show  a  list. In that list, choose System.Management and click the "OK" button.

4.jpeg

Now go to code view and write following code:


using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Management;
using
System.Data;
public
partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("User name", typeof(string)));
        dt.Columns.Add(new DataColumn("Account Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Caption", typeof(string)));
        dt.Columns.Add(new DataColumn("Description", typeof(string)));
        dt.Columns.Add(new DataColumn("Disabled", typeof(string)));
        dt.Columns.Add(new DataColumn("Domain", typeof(string)));
        dt.Columns.Add(new DataColumn("Instal Date", typeof(string)));
        dt.Columns.Add(new DataColumn("Local Account", typeof(string)));
        dt.Columns.Add(new DataColumn("Lockout", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Changeable", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Expires", typeof(string)));
        dt.Columns.Add(new DataColumn("Password Required", typeof(string)));
        dt.Columns.Add(new DataColumn("SID", typeof(string)));
        dt.Columns.Add(new DataColumn("SID Type", typeof(string)));
        dt.Columns.Add(new DataColumn("Status", typeof(string)));
        SelectQuery Sq = new SelectQuery("Win32_UserAccount");
        ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
        ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
        foreach (ManagementObject MO in osDetailsCollection)
        {
            DataRow dr = dt.NewRow();
            dr["User name"] = MO["FullName"].ToString();
            dr["Account Type"] = MO["AccountType"].ToString();
            dr["Caption"] = MO["Caption"];
            dr["Description"] = MO["Description"];
            dr["Disabled"] = MO["Disabled"];
            dr["Domain"] = MO["Domain"];
            dr["Instal Date"] = MO["InstallDate"];
            dr["Local Account"] = MO["LocalAccount"];
            dr["Lockout"] = MO["Lockout"];
            dr["Password Changeable"] = MO["PasswordChangeable"];
            dr["Password Expires"] = MO["PasswordExpires"];
            dr["Password Required"] = MO["PasswordRequired"];
            dr["SID"] = MO["SID"];
            dr["SID Type"] = MO["SIDType"];
            dr["Status"] = MO["Status"];
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
}
In the code above I get the information from Win32_UserAccount and store it in a datatable and bind it to the grid.

SelectQueryIt Represents a WQL (WMI Query Language) SELECT data query.

ManagementObjectSearcherIt retrieves a collection of management objects based on a specified query.

This class is one of the more commonly used entry points to retrieving management information.

ManagementObjectCollectionIt represents various collections of management objects retrieved using WMI.

Now build your application, it will show all intranet userinfo in the grid.



5.jpg 

To download the source click here