Hi,
I'm looking for some help on a small program that I'm trying to develop in
VB.NET.
The program I'm trying to develop needs to be able to do the following:
- Select remote server
- Select from two specific services
- Check the status of the server
- Ability to stop/start selected service.
The problem that I've got is that the status only returns a numerical code
instead of "Running" or "Stopped" etc, and the status doesn't always appear
in my textbox. I also get a return value stating the service is stopped when
it is in fact started!!
Any help would be much appreciated as this is driving me mad :-)
Thanks
Scott
----------------------------------------------------
Imports System.ServiceP rocess
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.
InitializeCompo nent()
'Add any initialization after the InitializeCompo nent() 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.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.Componen tModel.IContain er
'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 lblServiceStatu s 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.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
Dim resources As System.Resource s.ResourceManag er = New
System.Resource s.ResourceManag er(GetType(Form 1))
Me.comboBoxServ er = New System.Windows. Forms.ComboBox( )
Me.Label1 = New System.Windows. Forms.Label()
Me.lblServiceSt atus = 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.comboBoxServ ice = New System.Windows. Forms.ComboBox( )
Me.btnStopServi ce = New System.Windows. Forms.Button()
Me.btnStartServ ice = 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.SuspendLayou t()
'
'comboBoxServer
'
Me.comboBoxServ er.DropDownStyl e =
System.Windows. Forms.ComboBoxS tyle.DropDownLi st
Me.comboBoxServ er.Items.AddRan ge(New Object() {"Server1",
"Server2"})
Me.comboBoxServ er.Location = New System.Drawing. Point(104, 24)
Me.comboBoxServ er.Name = "comboBoxServer "
Me.comboBoxServ er.Size = New System.Drawing. Size(248, 21)
Me.comboBoxServ er.Sorted = True
Me.comboBoxServ er.TabIndex = 0
'
'Label1
'
Me.Label1.Locat ion = New System.Drawing. Point(8, 24)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing. Size(88, 23)
Me.Label1.TabIn dex = 1
Me.Label1.Text = "Remote Server:"
Me.Label1.TextA lign = System.Drawing. ContentAlignmen t.MiddleLeft
'
'lblServiceStat us
'
Me.lblServiceSt atus.Location = New System.Drawing. Point(104, 88)
Me.lblServiceSt atus.Name = "lblServiceStat us"
Me.lblServiceSt atus.Size = New System.Drawing. Size(160, 24)
Me.lblServiceSt atus.TabIndex = 2
Me.lblServiceSt atus.TextAlign =
System.Drawing. ContentAlignmen t.MiddleLeft
'
'Button1
'
Me.Button1.Loca tion = New System.Drawing. Point(232, 128)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing. Size(56, 23)
Me.Button1.TabI ndex = 3
Me.Button1.Text = "Status"
'
'lblService
'
Me.lblService.L ocation = New System.Drawing. Point(8, 88)
Me.lblService.N ame = "lblService "
Me.lblService.S ize = New System.Drawing. Size(80, 24)
Me.lblService.T abIndex = 4
Me.lblService.T ext = "Service Status:"
Me.lblService.T extAlign = System.Drawing. ContentAlignmen t.MiddleLeft
'
'Label2
'
Me.Label2.Locat ion = New System.Drawing. Point(8, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing. Size(88, 23)
Me.Label2.TabIn dex = 7
Me.Label2.Text = "Remote Service:"
Me.Label2.TextA lign = System.Drawing. ContentAlignmen t.MiddleLeft
'
'comboBoxServic e
'
Me.comboBoxServ ice.Items.AddRa nge(New Object() {"Service 1",
"Service 2"})
Me.comboBoxServ ice.Location = New System.Drawing. Point(104, 56)
Me.comboBoxServ ice.Name = "comboBoxServic e"
Me.comboBoxServ ice.Size = New System.Drawing. Size(248, 21)
Me.comboBoxServ ice.TabIndex = 8
'
'btnStopService
'
Me.btnStopServi ce.Location = New System.Drawing. Point(104, 128)
Me.btnStopServi ce.Name = "btnStopService "
Me.btnStopServi ce.Size = New System.Drawing. Size(56, 23)
Me.btnStopServi ce.TabIndex = 9
Me.btnStopServi ce.Text = "Stop"
'
'btnStartServic e
'
Me.btnStartServ ice.Location = New System.Drawing. Point(168, 128)
Me.btnStartServ ice.Name = "btnStartServic e"
Me.btnStartServ ice.Size = New System.Drawing. Size(56, 23)
Me.btnStartServ ice.TabIndex = 10
Me.btnStartServ ice.Text = "Start"
'
'MainMenu1
'
Me.MainMenu1.Me nuItems.AddRang e(New System.Windows. Forms.MenuItem( )
{Me.MenuItem1, Me.MenuItem3})
'
'MenuItem1
'
Me.MenuItem1.In dex = 0
Me.MenuItem1.Me nuItems.AddRang e(New System.Windows. Forms.MenuItem( )
{Me.MenuItem2})
Me.MenuItem1.Te xt = "File"
'
'MenuItem2
'
Me.MenuItem2.In dex = 0
Me.MenuItem2.Te xt = "Exit"
'
'MenuItem3
'
Me.MenuItem3.In dex = 1
Me.MenuItem3.Me nuItems.AddRang e(New System.Windows. Forms.MenuItem( )
{Me.MenuItem4, Me.MenuItem5})
Me.MenuItem3.Te xt = "About"
'
'MenuItem4
'
Me.MenuItem4.In dex = 0
Me.MenuItem4.Te xt = "Help"
'
'MenuItem5
'
Me.MenuItem5.In dex = 1
Me.MenuItem5.Te xt = "About"
'
'btnExit
'
Me.btnExit.Loca tion = New System.Drawing. Point(296, 128)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing. Size(56, 23)
Me.btnExit.TabI ndex = 11
Me.btnExit.Text = "Exit"
'
'Form1
'
Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)
Me.ClientSize = New System.Drawing. Size(376, 163)
Me.Controls.Add Range(New System.Windows. Forms.Control() {Me.btnExit,
Me.btnStartServ ice, Me.btnStopServi ce, Me.comboBoxServ ice, Me.Label2,
Me.lblService, Me.Button1, Me.lblServiceSt atus, Me.Label1,
Me.comboBoxServ er})
Me.FormBorderSt yle =
System.Windows. Forms.FormBorde rStyle.FixedDia log
Me.Icon = CType(resources .GetObject("$th is.Icon"),
System.Drawing. Icon)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.StartPositio n =
System.Windows. Forms.FormStart Position.Center Screen
Me.Text = "Control Panel"
Me.ResumeLayout (False)
End Sub
#End Region
Dim serviceControl As New ServiceControll er()
Dim serviceStatus As String
Dim remoteServer As String
Dim remoteService As String
Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
comboBoxServer. SelectedIndex = 1
End Sub
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
remoteServer = comboBoxServer. SelectedItem
remoteService = comboBoxService .SelectedItem
serviceControl. MachineName = remoteServer
serviceControl. ServiceName = remoteService
serviceStatus = serviceControl. Status
lblServiceStatu s.Text = serviceStatus
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
Private Sub MenuItem2_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MenuItem2.Click
Me.Close()
End Sub
Private Sub MenuItem4_Click (ByVal sender As System.Object, ByVal e As
System.EventArg s) 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.EventArg s) Handles btnStopService. Click
Try
lblServiceStatu s.Text = "Stopping service"
serviceControl. Stop()
lblServiceStatu s.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.EventArg s) 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.EventArg s) Handles btnStartService .Click
Try
lblServiceStatu s.Text = "Starting service"
serviceControl. Start()
lblServiceStatu s.Text = "Service started"
Catch serviceControl As Exception
MessageBox.Show ("Unable to start service")
End Try
End Sub
End Class