Mar 2, 2010

Load list of services

This code will load a list of all Services

private ServiceController msvc;
private ListViewItem ViewItem;
private Hashtable mcolSvcs = new Hashtable();
private bool fUpdatingUI ;

private void frmMain_Load(object sender, System.EventArgs e)
{
EnumServices();
}

private void EnumServices()
{
// Get the list of available services and load the list view control with the information
try
{
fUpdatingUI = true;
this.lvServices.Items.Clear();
if (!(mcolSvcs == null))
{
mcolSvcs = new Hashtable();
}
ServiceController[] svcs = ServiceController.GetServices();
foreach(ServiceController svc in svcs)
{
ViewItem = this.lvServices.Items.Add(svc.DisplayName);
ViewItem.SubItems.Add(svc.Status.ToString());
ViewItem.SubItems.Add(svc.ServiceType.ToString());
mcolSvcs.Add(svc.DisplayName,svc);
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message, exp.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
fUpdatingUI = false;
}
}

No comments:

Post a Comment