Manipulating Windows Services-VBForums
Results 1 to 2 of 2

Thread: Manipulating Windows Services

  1. #1

    Thread Starter
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,983

    Manipulating Windows Services

    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:
    1. Public Sub ServiceControl(ByVal ServiceName As String, ByVal StartupType As String)
    2.             Dim regKey As Microsoft.Win32.RegistryKey
    3.             Dim systm As Microsoft.Win32.RegistryKey
    4.             Dim curr As Microsoft.Win32.RegistryKey
    5.             Dim serv As Microsoft.Win32.RegistryKey
    6.             Dim mess As Microsoft.Win32.RegistryKey
    7.             regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SYSTEM")
    8.             systm = regKey.OpenSubKey("CurrentControlSet")
    9.             curr = systm.OpenSubKey("Services")
    10.             mess = curr.OpenSubKey(ServiceName, True)
    11.             Dim StartupInt As Integer
    12.             If UCase(StartupType) = "AUTOMATIC" Then
    13.                 StartupInt = 2
    14.             ElseIf UCase(StartupType) = "MANUAL" Then
    15.                 StartupInt = 3
    16.             ElseIf UCase(StartupType) = "DISABLE" Then
    17.                 StartupInt = 4
    18.             End If
    19.             mess.SetValue("Start", StartupInt)
    20.             mess.Close()
    21.         End Sub

    Starting and Stopping Services is fairly easy as well.

    VB Code:
    1. Public Sub ServiceStop(ByVal ServiceName As String)
    2.             Dim ServiceMessenger As New System.ServiceProcess.ServiceController(ServiceName)
    3.             If ServiceMessenger.CanStop Then
    4.                 ServiceMessenger.Stop()
    5.             End If
    6.         End Sub
    That will stop a service if it CAN be stopped.

    VB Code:
    1. Public Sub ServiceStart(ByVal ServiceName As String)
    2.             Dim ServiceMessenger As New System.ServiceProcess.ServiceController(ServiceName)
    3.             If ServiceMessenger.CanPauseAndContinue = True Then
    4.                 ServiceMessenger.Start()
    5.             End If
    6.         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.

    If you got anything to add to my code, or any changes needed, please post

  2. #2
    Lively Member
    Join Date
    Jan 2003
    Posts
    69
    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.