[RESOLVED] Stopping a Service using VB.Net
Hi guys
I know the title of this thread is a show stopper for a lot of us :D. But what I want to do is stop two services from running for a period of time then restart them. I know I can do this from the command prompt by using net stop servicename then restart with net start servicename. I need these two services stopped to do some work on the database. I do this manually right now and want to automate the process. I tried process.start with the command net stop but that did not work out. If I try the Process List I get the process running but not the services.
I'm actually doing this from a Script Task in SSIS (the script task uses VB.Net 2008).
Any help please
I'll understand if people don't want to reply as this can be used for things that we all try to prevent, but I figured I'd take a shot.
Gary
Re: Stopping a Service using VB.Net
Here are the 2 subroutines I use. You call them with StopService("servicename") and StartService("servicename")
Code:
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
Re: Stopping a Service using VB.Net
http://msdn.microsoft.com/en-us/libr...ontroller.aspx
vb.net Code:
' Use Imports System.ServiceProcess - manually add a referemce to System.ServiceProcess.dll
Private Sub StopService(ByVal servicename As String)
Dim myService As ServiceController =
(From svc In ServiceController.GetServices() Where svc.ServiceName = servicename).ToArray()(0)
If myService.Status = ServiceControllerStatus.Running AndAlso myService.CanStop Then
myService.Stop()
myService.WaitForStatus(ServiceControllerStatus.Stopped)
myService.Start()
End If
End Sub
Re: Stopping a Service using VB.Net
What does
Code:
Dim DataSource As String = txtServer.Text
represent? Do I need the machine name here?
Re: [RESOLVED] Stopping a Service using VB.Net
Thanks guys working for me now in SSIS.