Windows Services Status

S

Scott Davies

Hi,

I'm looking for some help on a small program that I'm trying to develop in
VB.NET. I'm having trouble getting the code that I've written to work, can
anyone shed some light as to where I'm going wrong.

The program has been written to do the following tasks:

- Select remote server
- Select from two specific services
- Check the status of the server
- Ability to stop/start selected service.

I've listed the code below from mainFrm.vb:

Any help would be much appreciated.

Thanks

Scott

----------------------------------------------------

Imports System.ServiceProcess

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents comboBoxServer As System.Windows.Forms.ComboBox
Friend WithEvents comboBoxService As System.Windows.Forms.ComboBox
Friend WithEvents lblServiceStatus As System.Windows.Forms.Label
Friend WithEvents lblService As System.Windows.Forms.Label
Friend WithEvents btnStopService As System.Windows.Forms.Button
Friend WithEvents btnStartService As System.Windows.Forms.Button
Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.Resources.ResourceManager = New
System.Resources.ResourceManager(GetType(Form1))
Me.comboBoxServer = New System.Windows.Forms.ComboBox()
Me.Label1 = New System.Windows.Forms.Label()
Me.lblServiceStatus = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.lblService = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.comboBoxService = New System.Windows.Forms.ComboBox()
Me.btnStopService = New System.Windows.Forms.Button()
Me.btnStartService = New System.Windows.Forms.Button()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.MenuItem1 = New System.Windows.Forms.MenuItem()
Me.MenuItem2 = New System.Windows.Forms.MenuItem()
Me.MenuItem3 = New System.Windows.Forms.MenuItem()
Me.MenuItem4 = New System.Windows.Forms.MenuItem()
Me.MenuItem5 = New System.Windows.Forms.MenuItem()
Me.btnExit = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'comboBoxServer
'
Me.comboBoxServer.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList
Me.comboBoxServer.Items.AddRange(New Object() {"Server1",
"Server2"})
Me.comboBoxServer.Location = New System.Drawing.Point(104, 24)
Me.comboBoxServer.Name = "comboBoxServer"
Me.comboBoxServer.Size = New System.Drawing.Size(248, 21)
Me.comboBoxServer.Sorted = True
Me.comboBoxServer.TabIndex = 0
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(88, 23)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Remote Server:"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'lblServiceStatus
'
Me.lblServiceStatus.Location = New System.Drawing.Point(104, 88)
Me.lblServiceStatus.Name = "lblServiceStatus"
Me.lblServiceStatus.Size = New System.Drawing.Size(160, 24)
Me.lblServiceStatus.TabIndex = 2
Me.lblServiceStatus.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(232, 128)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(56, 23)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Status"
'
'lblService
'
Me.lblService.Location = New System.Drawing.Point(8, 88)
Me.lblService.Name = "lblService"
Me.lblService.Size = New System.Drawing.Size(80, 24)
Me.lblService.TabIndex = 4
Me.lblService.Text = "Service Status:"
Me.lblService.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(8, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(88, 23)
Me.Label2.TabIndex = 7
Me.Label2.Text = "Remote Service:"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'comboBoxService
'
Me.comboBoxService.Items.AddRange(New Object() {"Service 1",
"Service 2"})
Me.comboBoxService.Location = New System.Drawing.Point(104, 56)
Me.comboBoxService.Name = "comboBoxService"
Me.comboBoxService.Size = New System.Drawing.Size(248, 21)
Me.comboBoxService.TabIndex = 8
'
'btnStopService
'
Me.btnStopService.Location = New System.Drawing.Point(104, 128)
Me.btnStopService.Name = "btnStopService"
Me.btnStopService.Size = New System.Drawing.Size(56, 23)
Me.btnStopService.TabIndex = 9
Me.btnStopService.Text = "Stop"
'
'btnStartService
'
Me.btnStartService.Location = New System.Drawing.Point(168, 128)
Me.btnStartService.Name = "btnStartService"
Me.btnStartService.Size = New System.Drawing.Size(56, 23)
Me.btnStartService.TabIndex = 10
Me.btnStartService.Text = "Start"
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.MenuItem1, Me.MenuItem3})
'
'MenuItem1
'
Me.MenuItem1.Index = 0
Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.MenuItem2})
Me.MenuItem1.Text = "File"
'
'MenuItem2
'
Me.MenuItem2.Index = 0
Me.MenuItem2.Text = "Exit"
'
'MenuItem3
'
Me.MenuItem3.Index = 1
Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem()
{Me.MenuItem4, Me.MenuItem5})
Me.MenuItem3.Text = "About"
'
'MenuItem4
'
Me.MenuItem4.Index = 0
Me.MenuItem4.Text = "Help"
'
'MenuItem5
'
Me.MenuItem5.Index = 1
Me.MenuItem5.Text = "About"
'
'btnExit
'
Me.btnExit.Location = New System.Drawing.Point(296, 128)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(56, 23)
Me.btnExit.TabIndex = 11
Me.btnExit.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(376, 163)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnExit,
Me.btnStartService, Me.btnStopService, Me.comboBoxService, Me.Label2,
Me.lblService, Me.Button1, Me.lblServiceStatus, Me.Label1,
Me.comboBoxServer})
Me.FormBorderStyle =
System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Icon = CType(resources.GetObject("$this.Icon"),
System.Drawing.Icon)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Control Panel"
Me.ResumeLayout(False)

End Sub

#End Region

Dim serviceControl As New ServiceController()
Dim serviceStatus As String
Dim remoteServer As String
Dim remoteService As String

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
comboBoxServer.SelectedIndex = 1
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
remoteServer = comboBoxServer.SelectedItem
remoteService = comboBoxService.SelectedItem
serviceControl.MachineName = remoteServer
serviceControl.ServiceName = remoteService
serviceStatus = serviceControl.Status
lblServiceStatus.Text = serviceStatus
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub MenuItem2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem2.Click
Me.Close()
End Sub

Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem4.Click
MessageBox.Show("The Help function is unavailable in this version.",
"Help")
End Sub

Private Sub btnStopService_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStopService.Click
Try
lblServiceStatus.Text = "Stopping service"
serviceControl.Stop()
lblServiceStatus.Text = "Service stopped"
Catch serviceControl As Exception
MessageBox.Show("Unable to stop service")
End Try
End Sub

Private Sub MenuItem5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MenuItem5.Click
MessageBox.Show("2003 - Developed by xxxx", "About")
End Sub

Private Sub btnStartService_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles btnStartService.Click
Try
lblServiceStatus.Text = "Starting service"
serviceControl.Start()
lblServiceStatus.Text = "Service started"
Catch serviceControl As Exception
MessageBox.Show("Unable to start service")
End Try
End Sub
End Class
 
A

Armin Zingler

Scott Davies said:
I'm looking for some help on a small program that I'm trying to
develop in VB.NET. I'm having trouble getting the code that I've
written to work, can anyone shed some light as to where I'm going
wrong.

_What_ is going wrong?
The program has been written to do the following tasks:

- Select remote server
- Select from two specific services
- Check the status of the server
- Ability to stop/start selected service.

I've listed the code below from mainFrm.vb:

After reformatting the code and setting a reference to
System.ServiceProcess.dll, three errors remain. I guess you don't use option
strict (but you should). I don't see more errors.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top