Description
This program manages services running in your computer. You can use the GetServices method on the ServiceController class to retrieve a list of the services on a particular computer. The GetServices method returns an array of all of a computer's available services, except for those associated with device drivers.
Source Code:
// Source Code starts
protected void PopulateTreeView()
{
TreeNode RootNode = new TreeNode("Services",0,0);
treeService.Nodes.Add(RootNode);
//Call GetServices on the ServiceController class.
//GetServices returns an array of ServiceController
//objects.
foreach(ServiceController service in services)
{
TreeNode ServiceNode = new TreeNode(service.ServiceName);
RootNode.Nodes.Add(ServiceNode);
}
treeService.ExpandAll();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void treeService_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
lstDependedService.Items.Clear();
lstServicesDependedOn.Items.Clear();
foreach(ServiceController service in services)
{
if(service.ServiceName==e.Node.Text)
{
SelectedService=service;
FillForm(service);
break;
}
}
//MessageBox.Show(((TreeView)sender).SelectedNode.Text);
}
private void FillForm(ServiceController service)
{
lblServiceName.Text=service.ServiceName;
lblCanPauseAndContinue.Text=service.CanPauseAndContinue.ToString();
lblCanShutdown.Text=service.CanShutdown.ToString();
lblDisplayName.Text=service.DisplayName;
lblServiceType.Text=service.ServiceType.ToString();
lblStatus.Text=service.Status.ToString();
lblStop.Text=service.CanStop.ToString();
foreach(ServiceController dependent in service.DependentServices)
{
lstDependedService.Items.Add(dependent.ServiceName);
}
foreach(ServiceController dependsOn in service.ServicesDependedOn)
{
lstServicesDependedOn.Items.Add(dependsOn.ServiceName);
}
}
private void menuStart_Click(object sender, System.EventArgs e)
{
if(SelectedService.Status.ToString()=="Stopped")
{
SelectedService.Start();
SelectedService.Refresh();
PrepareForm(SelectedService);
}
else
{
MessageBox.Show("You cannot use Start menu because " + SelectedService.ServiceName+" is
+SelectedService.Status.ToString());
}
}
private void menuStop_Click(object sender, System.EventArgs e)
{
if(SelectedService.CanStop==true)
{
if(SelectedService.Status.ToString()!="Stopped")
{
SelectedService.Stop();
SelectedService.Refresh();
PrepareForm(SelectedService);
}
else
{
MessageBox.Show("You cannot use Stop menu because " + SelectedService.ServiceName+" is
+SelectedService.Status.ToString());
}
}
else
{
MessageBox.Show("You cannot use Stop menu because " + SelectedService.ServiceName+" does not support Stop");
}
}
private void menuPause_Click(object sender, System.EventArgs e)
{
if(SelectedService.CanPauseAndContinue==true)
{
if(SelectedService.Status.ToString()=="Running")
{
SelectedService.Pause();
SelectedService.Refresh();
PrepareForm(SelectedService);
}
else
{
MessageBox.Show("You cannot use Pause menu because " + SelectedService.ServiceName+" is
+SelectedService.Status.ToString());
}
}
else
{
MessageBox.Show("You cannot use Pause menu because " + SelectedService.ServiceName+" does not support PauseAndContinue");
}
}
private void menuContinue_Click(object sender, System.EventArgs e)
{
if(SelectedService.CanPauseAndContinue==true)
{
if(SelectedService.Status.ToString()=="Paused")
{
SelectedService.Continue();
SelectedService.Refresh();
PrepareForm(SelectedService);
}
else
{
MessageBox.Show("You cannot use Continue menu because " + SelectedService.ServiceName+" is
+SelectedService.Status.ToString());
}
}
else
{
MessageBox.Show("You cannot use Continue menu because " + SelectedService.ServiceName+" does not support
PauseAndContinue");
}
}
private void menuRefresh_Click(object sender, System.EventArgs e)
{
SelectedService.Refresh();
PrepareForm(SelectedService);
}
private void PrepareForm(ServiceController service)
{
lstDependedService.Items.Clear();
lstServicesDependedOn.Items.Clear();
FillForm(service);
}
private void lblStatus_Click(object sender, System.EventArgs e)
{
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
private void lblStop_Click(object sender, System.EventArgs e)
{
}
private void lblCanShutdown_Click(object sender, System.EventArgs e)
{
}
private void lblServiceName_Click(object sender, System.EventArgs e)
{
}
private void lblServiceType_Click(object sender, System.EventArgs e)
{
}
private void menuExit_Click(object sender, System.EventArgs e)
{
Application.Exit();
}
private void menuAbout_Click(object sender, System.EventArgs e)
{
new About().ShowDialog();
}
// Source Code End