Tuesday, January 26, 2010

Finding Computer Information with WMI

Using WMI you can find various information about a HOST PC. In C# some simple calls can pull up various details in regards to a PC including OS info, Memory information, Description, Manufacturer, Model and various other details.


private void ComputerDetails(

string temp1)

{

if (this.treePing.SelectedNode.ForeColor ==

Color.LawnGreen || this.treePing.SelectedNode.ForeColor ==

Color.Black)

{

toolStripStatusLabel1.Text = temp1 +

" Information: ";

try

{

ConnectionOptions opt = new ConnectionOptions();

ManagementPath p = new ManagementPath("\\\\" + temp1 + "\\root\\cimv2");

ConnectionOptions oConn = new ConnectionOptions();

oConn.Impersonation = ImpersonationLevel.Impersonate;

ManagementScope oMs =

new System.Management.ManagementScope(p, oConn);

System.Management.ObjectQuery oQuery =

new System.Management.ObjectQuery("select * from Win32_OperatingSystem");

System.Management.ObjectQuery oQuery2 =

new System.Management.ObjectQuery("select * from Win32_ComputerSystem");

System.Management.ObjectQuery oQuery3 =

new System.Management.ObjectQuery("select * from Win32_LogicalDisk");

ManagementObjectSearcher oSearcher =

new ManagementObjectSearcher(oMs, oQuery);

ManagementObjectSearcher oSearcher2 =

new ManagementObjectSearcher(oMs, oQuery2);

ManagementObjectSearcher oSearcher3 =

new ManagementObjectSearcher(oMs, oQuery3);

ManagementObjectCollection oReturnCollection = oSearcher.Get();

ManagementObjectCollection oReturnCollection2 = oSearcher2.Get();

ManagementObjectCollection oReturnCollection3 = oSearcher3.Get();

txtPing.Text = "-----------------------------------------------------";

txtPing.Text += "\r\n";

txtPing.Text += "Details: " + temp1;

txtPing.Text += "\r\n";

txtPing.Text += "-----------------------------------------------------";

txtPing.Text += "\r\n";

foreach (ManagementObject oReturn in oReturnCollection)

{

txtPing.Text += "Description: " + oReturn["Description"].ToString() + "\r\n";

foreach (ManagementObject oReturn2 in oReturnCollection2)

{

txtPing.Text += "Manufacturer: " + oReturn2["Manufacturer"].ToString() + "\r\n";

txtPing.Text += "Model: " + oReturn2["Model"].ToString() + "\r\n";

txtPing.Text += "System Type: " + oReturn2["systemtype"].ToString() + "\r\n";

txtPing.Text += "Total Physical Memory: " +

Convert.ToDecimal(oReturn2["TotalPhysicalMemory"]) / 1000000000 + " GB \r\n";

}

txtPing.Text += "Free Physical Memory: " +

Convert.ToDecimal(oReturn["FreePhysicalMemory"])/1000000 + " GB \r\n";

txtPing.Text += "Free Virtual Memory: " +

Convert.ToDecimal(oReturn["FreeVirtualMemory"])/1000000 + " GB \r\n";

//loop through found drives and write out info

foreach (ManagementObject manobj in oReturnCollection3)

{

double FreeSpace22 = Convert.ToDouble(manobj["FreeSpace"]) / 1000000000;

double DiskSize22 = Convert.ToDouble(manobj["Size"]) / 1000000000;

double freespace2 = double.Parse(FreeSpace22.ToString());

double disksize2 = double.Parse(DiskSize22.ToString());

double pctfree2 = freespace2 / disksize2;

int lengthdiff = disksize2.ToString("N0").Length -

freespace2.ToString("N0").Length;

txtPing.Text += "For drive: " + manobj["name"].ToString() + ": \r\n";

double FreeSpaceSize = Convert.ToDouble(manobj["Size"]) / 1000000000;

txtPing.Text += "Size: " + FreeSpaceSize.ToString() + " GB \r\n";

switch (lengthdiff)

{

case 0:

txtPing.Text += "Free space: " + freespace2.ToString("N0") +

" GB \r\n";

break;

case 1:

txtPing.Text += "Free space: " + freespace2.ToString("N0")

+ " GB \r\n";

break;

case 2:

txtPing.Text += "Free space: " + freespace2.ToString("N0")

+ " GB \r\n";

break;

case 3:

txtPing.Text += "Free space: " + freespace2.ToString("N0")

+ " GB \r\n";

break;

default:

txtPing.Text += "Free space: " + freespace2.ToString("N0")

+ " GB \r\n";

break;

}

if (pctfree2 < 0.10)

{

mnuNotify.ShowBalloonTip(

5000,

"NetPinger 2010: " + temp1,

temp1 + " has low disk space on drive " +

manobj["name"].ToString() +

" ~ " +

pctfree2.ToString("P"),

ToolTipIcon.Warning);

}

txtPing.Text += "Percentage Free: " + pctfree2.ToString("P") + "\r\n";

}

txtPing.Text += "Install Date/Time: " + ConvertDate(oReturn["InstallDate"].ToString()) + "\r\n";

txtPing.Text += "Last Bootup: " +

ConvertDate(oReturn["LastBootUpTime"].ToString()) + "\r\n";

txtPing.Text += @"Local Date/Time: " +

ConvertDate(oReturn["LocalDateTime"].ToString()) + "\r\n";

txtPing.Text += "Organization: " +

oReturn["Organization"].ToString() + "\r\n";

txtPing.Text += "Name: " + oReturn["Name"].ToString() + "\r\n";

txtPing.Text += "OS Language: " + oReturn["OSLanguage"].ToString() + "\r\n";

txtPing.Text += "OS Type: " + oReturn["OSType"].ToString() + "\r\n";

txtPing.Text += "OS Directory: " + oReturn["WindowsDirectory"].ToString() + "\r\n";

txtPing.Text += "Registered User: " +

oReturn["RegisteredUser"].ToString() + "\r\n";

txtPing.Text += "Serial Number: " +

oReturn["SerialNumber"].ToString() + "\r\n";

txtPing.Text += "ServicePack Major Version: " +

oReturn["ServicePackMajorVersion"].ToString() + "\r\n";

txtPing.Text += "ServicePack Minor Version: " +

oReturn["ServicePackMinorVersion"].ToString() + "\r\n";

}

}

catch (Exception ex)

{

mnuNotify.ShowBalloonTip(

5000,

"NetPinger 2010: " + temp1,

ex.Message,

ToolTipIcon.Warning);

toolStripStatusLabel1.Text =

ex.Message + " , details aren't available";

}

}

else

{

txtPing.Text = "Sorry but device: " + temp1 + " is currenty unavilable";

}

}

private string ConvertDate(String date1)

{

// MessageBox.Show(date1);

String DateVal1 = date1.Substring(0, 4);

String DateVal2 = date1.Substring(4, 2);

String DateVal3 = date1.Substring(6, 2);

String DateVal4 = date1.Substring(8, 2);

String DateVal5 = date1.Substring(10, 2);

String DateVal =

DateVal2 + @"/" + DateVal3 + @"/" +

DateVal1 + ", " + DateVal4 + ":" +

DateVal5;

// MessageBox.Show(DateVal);

return DateVal;

}

No comments:

Post a Comment

Generating "Always On Top" NSWindow in macOS across all detected displays

Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift  Langues, you m...