Click here to Skip to main content
14,305,260 members
Rate this:
Please Sign up or sign in to vote.
See more:
how to start and stop service form web page in vb.net,i have a windows exe and install in machine ,it start its service and can see in service from control panel.i want to verify that service is running or not.if running i want to stop in one button ,in another button if service is stop it start,pls reply asap

thanx
Posted
Comments
Amir Mahfoozi 8-May-13 5:11am
   
Please read here :
http://www.csharp-examples.net/restart-windows-service/
Aravindba 8-May-13 23:37pm
   
Thanx.......

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 3

Imports System.ServiceProcess

Public Sub StopService(ByVal myServiceName As String)

    Dim DataSource As String = txtServer.Text
    Dim sStatus As String
    Dim myController As ServiceController

    myController = New ServiceController
    myController.MachineName = DataSource
    myController.ServiceName = myServiceName

    TextBox1.Text += "Stopping service """ & myServiceName & """...." & vbNewLine

    If myController.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
        TextBox1.Text += "Service """ & myServiceName & """ is already stopped" & vbNewLine
    Else

        Try
            myController.Refresh()
            sStatus = myController.Status.ToString
            myController.Stop()
            myController.WaitForStatus(ServiceControllerStatus.Stopped)
            TextBox1.Text += "Service """ & myServiceName & """ stopped..." & vbNewLine
        Catch exp As Exception
            TextBox1.Text += "Could not stop service """ & myServiceName & """" & vbNewLine
        End Try
    End If
End Sub


Public Sub StartService(ByVal myServiceName As String)

    Dim DataSource As String = txtServer.Text
    Dim sStatus As String
    Dim myController As ServiceController

    myController = New ServiceController
    myController.MachineName = DataSource
    myController.ServiceName = myServiceName

    TextBox1.Text += "Starting service """ & myServiceName & """...." & vbNewLine

    Try
        myController.Refresh()
        sStatus = myController.Status.ToString
        myController.Start()
        myController.WaitForStatus(ServiceControllerStatus.Running)
        TextBox1.Text += "Service """ & myServiceName & """ started..." & vbNewLine
    Catch exp As Exception
        TextBox1.Text += "Could not start service """ & myServiceName & """" & vbNewLine
    End Try

End Sub
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100