Sunday, February 7, 2010

View Services of Remote PC in WMI C#


This little segment will assist you in viewing running services on a remote PC in C#. It'll use your current credentials. Here is my NetPinger application screenshot. I just put the services feature that show the results in a text box!

try

{

ConnectionOptions connection = new ConnectionOptions();

ManagementScope scope = new ManagementScope(

"\\\\" + temp1 + "\\root\\CIMV2", connection);

scope.Connect();

ObjectQuery query = new ObjectQuery(

"SELECT * FROM Win32_Service");

ManagementObjectSearcher searcher =

new ManagementObjectSearcher(scope, query);

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

txtPing.Text += "Services on " + temp1;

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

foreach (ManagementObject queryObj in searcher.Get())

{

txtPing.Text +="Caption: {0}" + queryObj["Caption"] ;

txtPing.Text +="Description: {0}" + queryObj["Description"];

txtPing.Text +="Name: {0}"+ queryObj["Name"];

txtPing.Text +="PathName: {0}"+ queryObj["PathName"];

txtPing.Text +="State: {0}"+ queryObj["State"];

txtPing.Text +="Status: {0}"+ queryObj["Status"];

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

}

Close();

}

catch (ManagementException ex)

{

mnuNotify.ShowBalloonTip(

5000,

"NetPinger 2010: " + temp1,

ex.Message,

ToolTipIcon.Warning);

toolStripStatusLabel1.Text =

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

}

No comments:

Post a Comment