services agen...

jorge

Junior Contributor
Joined
Jul 13, 2003
Messages
239
Location
Belgium
Hey,
I'd like to make a list of all services,
Via a list box i pasible, somesaid use System.ServiceProcess.ServiceController.GetServices()
But don't have a clow howto!
Anyone?
 
just add a listbox to a form and from the components tab drop a service controller onto the form, then use this code in aform_load

Visual Basic:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim sc As System.ServiceProcess.ServiceController

		For Each sc In ServiceController1.GetServices
			ListBox1.Items.Add(sc.ServiceName)
		Next
	End Sub
 
Cool thanx
Can i filter them?
Like in the description need to be "some text"?
And set the listbox to select "apache2" as default?
 
Last edited:
The following will autoselect a particular item

Visual Basic:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim sc As System.ServiceProcess.ServiceController

		For Each sc In ServiceController1.GetServices
			ListBox1.Items.Add(sc.DisplayName)   'Edit: shows pretty name for service
		Next

		ListBox1.SelectedIndex = ListBox1.FindString("Indexing")    'Change "Indexing" to "Apache2" or whatever the service name is
	End Sub

To filter you could use regular expressions if you need complex matching or for a simple one something like
Visual Basic:
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim sc As System.ServiceProcess.ServiceController

		For Each sc In ServiceController1.GetServices
			If sc.DisplayName.StartsWith("In") Then
				ListBox1.Items.Add(sc.DisplayName)
			End If
		Next

		ListBox1.SelectedIndex = ListBox1.FindString("Indexing")
	End Sub
 
ok Thanx a lot:D
//edit: Can i filter on description?
Did not see iet anywhere.
//edit2: How do i clear the listbox?
 
Last edited:
Back
Top