Well I'm still a beginner at VB .NET but I figure this code would probably help people out.
If you want to set a service to automatic, manual or disable, you have to modify the registry. This Sub I created will take in the service's name (Not the display name) and either automatic, manual or disable and then write the appropriate registry value.
VB Code:
Public Sub ServiceControl(ByVal ServiceName As String, ByVal StartupType As String) Dim regKey As Microsoft.Win32.RegistryKey Dim systm As Microsoft.Win32.RegistryKey Dim curr As Microsoft.Win32.RegistryKey Dim serv As Microsoft.Win32.RegistryKey Dim mess As Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM") systm = regKey.OpenSubKey("CurrentControlSet") curr = systm.OpenSubKey("Services") mess = curr.OpenSubKey(ServiceName, True) Dim StartupInt As Integer If UCase(StartupType) = "AUTOMATIC" Then StartupInt = 2 ElseIf UCase(StartupType) = "MANUAL" Then StartupInt = 3 ElseIf UCase(StartupType) = "DISABLE" Then StartupInt = 4 End If mess.SetValue("Start", StartupInt) mess.Close() End Sub
Starting and Stopping Services is fairly easy as well.
That will stop a service if it CAN be stopped.VB Code:
Public Sub ServiceStop(ByVal ServiceName As String) Dim ServiceMessenger As New System.ServiceProcess.ServiceController(ServiceName) If ServiceMessenger.CanStop Then ServiceMessenger.Stop() End If End Sub
This will start the service if it can be Paused and Continued. You can change the CanPauseAndContinue to something else, I just wasn't sure of the best method and that seems to work.VB Code:
Public Sub ServiceStart(ByVal ServiceName As String) Dim ServiceMessenger As New System.ServiceProcess.ServiceController(ServiceName) If ServiceMessenger.CanPauseAndContinue = True Then ServiceMessenger.Start() End If End Sub
If you got anything to add to my code, or any changes needed, please post![]()


Reply With Quote
