SIGN UP MEMBER LOGIN:    
ARTICLE

Service Explorer

Posted by Levent Camlibel Articles | Windows Services in C# August 06, 2001
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.
Reader Level:
Download Files:
 

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

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Become a Sponsor