Retrieving software details from remote machine

Retrieving softwares details from remote machine




using System.Management;
using System.Threading;

DataTable oDt = new DataTable();


public Form1()
{
oDt.Columns.Add("System Name");
oDt.Columns.Add("Software/Hardware");
oDt.Columns.Add("Caption");
oDt.Columns.Add("Configuration");
}

private void btnLstSoftwares_Click(object sender, EventArgs e)
{
string[] sArr = null;
for (int i = 0; i < comboBox1.Items.Count; i++)
{
sArr = comboBox1.Items[i].ToString().Split('=');
ThreadPool.QueueUserWorkItem(new WaitCallback(MyThread), sArr[1].ToString());
}
}

public void MyThread(object oSysName)
{
lock (this)
{
string sSysName = (string)oSysName;
try
{
ConnectionOptions connection = new ConnectionOptions();
connection.Username = "administrator";
connection.Password = "admin";

ManagementScope scope = new ManagementScope("\\\\" + sSysName.ToString() + "\\root\\CIMV2", connection);
scope.Connect();
ObjectQuery queryProduct = new ObjectQuery("SELECT * FROM Win32_Product");
ManagementObjectSearcher searcherProduct = new ManagementObjectSearcher(scope, queryProduct);

foreach (ManagementObject queryObjSw in searcherProduct.Get())
{
DataRow oDr1;
oDr1 = oDt.NewRow();
oDr1[0] = sSysName;
oDr1[1] = "Software";
oDr1[2] = "Caption";
oDr1[3] = queryObjSw["Caption"].ToString();
oDt.Rows.Add(oDr1);
}
catch (ManagementException oMnEx)
{
DataRow oDr1;
oDr1 = oDt.NewRow();
oDr1[0] = sSysName;
oDr1[1] = "Error";
oDr1[2] = "";
oDr1[3] = oMnEx.Message.ToString();
oDt.Rows.Add(oDr1);
}
catch (System.UnauthorizedAccessException unauthorizedErr)
{
DataRow oDr1;
oDr1 = oDt.NewRow();
oDr1[0] = sSysName;
oDr1[1] = "Error";
oDr1[2] = "";
oDr1[3] = unauthorizedErr.Message.ToString();
oDt.Rows.Add(oDr1);

}
catch (Exception oEx)
{
DataRow oDr1;
oDr1 = oDt.NewRow();
oDr1[0] = sSysName;
oDr1[1] = "Error";
oDr1[2] = "";
oDr1[3] = oEx.Message.ToString();
oDt.Rows.Add(oDr1);

}
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = oDt;
}
}
}


Comments

Guest Author: Pandiarajan.C20 Sep 2012

This code takes very long time to get the list....
Don't use SELECT * FROM Win32_Product...
Please suggest some other coding....



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: