Click here to Skip to main content
15,886,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

VB
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900