windows services

Is there any articles on serviceController?

I am trying to create a program the starts and stops services in windows

I am looking for some code or some way to loop through services that are currently running on a either win2k or xp.

Once I have that I should be good.



malanoisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

iboutchkineCommented:
'Ref to System.ServiceProcesses.dll

Public Class frmMain
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Call ReadSvcs()
    End Sub

    Sub ReadSvcs()
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController
        Dim LVW As ListViewItem

        ListSvcs = SingleSvc.GetServices

        lvwServices.Items.Clear()
        Try
            For Each SingleSvc In ListSvcs
                LVW = lvwServices.Items.Add(SingleSvc.DisplayName)
                LVW.SubItems.Add(SingleSvc.ServiceName)
                LVW.SubItems.Add(SingleSvc.Status.ToString)
                LVW.SubItems.Add(SingleSvc.ServiceType.ToString)
            Next
        Catch e As Exception
            MessageBox.Show("Could not initialize Windows Service engine.  Restarting your computer may work", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Private Sub lvwServices_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwServices.Click 'parental
        If lvwServices.SelectedItems(0).Text <> "" Then
            Select Case lvwServices.SelectedItems(0).SubItems(2).Text
                Case "Stopped"
                    cmdStop.Enabled = False
                    cmdStart.Enabled = True

                Case "Running"
                    cmdStop.Enabled = True
                    cmdStart.Enabled = False

                Case Else
                    cmdStop.Enabled = False
                    cmdStart.Enabled = False
            End Select
        End If
    End Sub

    Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
        If lvwServices.SelectedItems(0).Text <> "" Then
            Call StopService(lvwServices.SelectedItems(0).SubItems(1).Text)
        End If 'It has
    End Sub

    Sub StopService(ByVal ServiceName As String)
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController

        ListSvcs = SingleSvc.GetServices

        Try
            For Each SingleSvc In ListSvcs
                If UCase(SingleSvc.ServiceName) = UCase(ServiceName) Then
                    SingleSvc.Stop()
                    Call ReadSvcs()
                    Exit For
                End If
            Next
        Catch e As Exception
            MessageBox.Show("Could not stop service.  Ensure it is not disabled", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Sub StartService(ByVal ServiceName As String)
        Dim ListSvcs() As ServiceProcess.ServiceController
        Dim SingleSvc As ServiceProcess.ServiceController

        ListSvcs = SingleSvc.GetServices

        Try
            For Each SingleSvc In ListSvcs
                If UCase(SingleSvc.ServiceName) = UCase(ServiceName) Then
                    SingleSvc.Start()
                    Call ReadSvcs()
                    Exit For
                End If
            Next
        Catch e As Exception
            MessageBox.Show("Could not start service.  Ensure it is not disabled", "Fatal Error: " & e.Source)
        End Try
    End Sub

    Private Sub cmdStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click
        If lvwServices.SelectedItems(0).Text <> "" Then
            Call StartService(lvwServices.SelectedItems(0).SubItems(1).Text)
        End If
    End Sub

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        End
    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        Call ReadSvcs()
    End Sub

End Class
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
malanoisAuthor Commented:
ok,

I have created a new project, copied and pasted the above code.
added button for , start, stop,refresh, and exit
added a listview

the first several tries I get an error for:
 lvwServices.Items.Clear()


I have rebooted several times and I get the same error everytime:

("Could not initialize Windows Service engine.  Restarting your computer may work"

XP srv Pk1

MJ
0
malanoisAuthor Commented:
good now
 Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.