ARTICLE

Service Management in VB.NET

Posted by Sateesh Arveti Articles | VB.NET Articles December 29, 2006
This article discusses how to create a Web Application to manage the services running on our system.
Download Files:
 
Reader Level:

All of us know there are so many services running in background without our knowledge. We can view, what are the services running on our system by typing services.msc in Run of Start Button.

I will explain how to create a Web Application to manage the services running on our system. This application allows us to see all services present in our system. We can also start, stop, or pause the services. This application will show some details of all services, like its path, state... I created this application in VS.NET 2003.

First create a web application in VB.NET and Name it as ServiceMgmt.

Design the UI as shown below:

There are 4 linkbuttons, one dropdownlist and 7 labels in it.

Add reference to System.Management(in Solution Explorer) as shown below:

Next,

 

Add this code in Page_Load :

Dim class1 As ManagementClass = New ManagementClass("Win32_Service")

For Each ob As ManagementObject In class1.GetInstances

    Dim item1 As ListItem = New ListItem

    item1.Text = ob.GetPropertyValue("Name").ToString

    item1.Value = ob.GetPropertyValue("Caption").ToString

    DropDownList1.Items.Add(item1)

Next

This will load all the services into the ComboBox.

Next add this method:

Private Sub showstatus()

    Dim i As Integer = 0

    Dim class1 As ManagementClass = New ManagementClass("Win32_Service")

    For Each ob As ManagementObject In class1.GetInstances

        If ob.GetPropertyValue("State").ToString = "Running" Then

            System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)

        End If

        If ob.GetPropertyValue("Name").ToString = DropDownList1.SelectedItem.Text.ToString Then

            Label2.Text = "Description : " + ob.GetPropertyValue("Description")

            Label3.Text = "PathName : " + ob.GetPropertyValue("PathName")

            Label4.Text = "ServiceType : " + ob.GetPropertyValue("ServiceType")

            Label5.Text = "StartMode : " + ob.GetPropertyValue("StartMode")

            Label6.Text = "State : " + ob.GetPropertyValue("State")

        End If

    Next

    Label7.Text = String.Empty

    Label7.Text = "Total Number of Services Running : " + i

End Sub

 

This will be called whenever you select a service from combobox. This will show some of the details of the service.

Next add this method:

Private Sub servicemgmt(ByVal status As String)

    Dim path As ManagementPath = New ManagementPath

    path.Server = System.Environment.MachineName

    path.NamespacePath = "root\CIMV2"

    path.RelativePath = "Win32_service.Name='" + DropDownList1.SelectedItem.Text.ToString + "'"

    Dim service As ManagementObject = New ManagementObject(path)

    Dim temp As ManagementBaseObject = service.InvokeMethod(status, Nothing, Nothing)

End Sub

This method will start, stop, pause, continue the service you selected from combobox.

Next copy this code to Start LinkButton_Click:

servicemgmt("StartService")

showstatus()

Next copy this code to Stop LinkButton_Click:

servicemgmt("StopService")

showstatus()

Next copy this code to Pause LinkButton_Click:

servicemgmt("PauseService")
showstatus()

Next copy this code to Continue LinkButton_Click:

servicemgmt("ResumeService")
showstatus()

By this, we can control our services from IE.

I hope this code will be useful for all. Final screen will be like this:

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (http://www.c-sharpcorner.com/).

Login to add your contents and source code to this article
share this article :
post comment
 

Hi,

It's very useful for our requirement. But i am not able to start,stop  service using this code.

Please help me

Best regards
Sunil
sunil_forjob@yahoo.com

Posted by Sunil P May 12, 2010
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.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor