⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 Visual.Basic.NET实用编程百例-47.6M.zip
💻 VB
字号:
Imports System.ServiceProcess
Public Class Form1
    Inherits System.Windows.Forms.Form
    Private DisplayName As String
    Private Listcurrent As Object
    Private ComputerName As String

#Region " Windows 窗体设计器生成的代码 "

    Public Sub New()
        MyBase.New()

        '该调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        '在 InitializeComponent() 调用之后添加任何初始化

    End Sub

    '窗体重写 dispose 以清理组件列表。
    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
    Friend WithEvents ServiceController1 As System.ServiceProcess.ServiceController
    Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents ListBox2 As System.Windows.Forms.ListBox
    Friend WithEvents ListBox3 As System.Windows.Forms.ListBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents Button1 As System.Windows.Forms.Button
    'Windows 窗体设计器所必需的
    Private components As System.ComponentModel.IContainer

    '注意: 以下过程是 Windows 窗体设计器所必需的
    '可以使用 Windows 窗体设计器修改此过程。
    '不要使用代码编辑器修改它。
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ListBox1 = New System.Windows.Forms.ListBox
        Me.Label1 = New System.Windows.Forms.Label
        Me.Button2 = New System.Windows.Forms.Button
        Me.ListBox2 = New System.Windows.Forms.ListBox
        Me.ListBox3 = New System.Windows.Forms.ListBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.Label3 = New System.Windows.Forms.Label
        Me.Button4 = New System.Windows.Forms.Button
        Me.Button3 = New System.Windows.Forms.Button
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.ItemHeight = 12
        Me.ListBox1.Location = New System.Drawing.Point(80, 24)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.ScrollAlwaysVisible = True
        Me.ListBox1.Size = New System.Drawing.Size(296, 88)
        Me.ListBox1.TabIndex = 0
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(8, 40)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(72, 23)
        Me.Label1.TabIndex = 1
        Me.Label1.Text = "启动的服务"
        '
        'Button2
        '
        Me.Button2.Enabled = False
        Me.Button2.Location = New System.Drawing.Point(112, 328)
        Me.Button2.Name = "Button2"
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "启动服务"
        '
        'ListBox2
        '
        Me.ListBox2.ItemHeight = 12
        Me.ListBox2.Location = New System.Drawing.Point(80, 120)
        Me.ListBox2.Name = "ListBox2"
        Me.ListBox2.ScrollAlwaysVisible = True
        Me.ListBox2.Size = New System.Drawing.Size(296, 88)
        Me.ListBox2.TabIndex = 5
        '
        'ListBox3
        '
        Me.ListBox3.ItemHeight = 12
        Me.ListBox3.Location = New System.Drawing.Point(80, 216)
        Me.ListBox3.Name = "ListBox3"
        Me.ListBox3.ScrollAlwaysVisible = True
        Me.ListBox3.Size = New System.Drawing.Size(296, 88)
        Me.ListBox3.TabIndex = 6
        '
        'Label2
        '
        Me.Label2.Location = New System.Drawing.Point(8, 128)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(72, 23)
        Me.Label2.TabIndex = 7
        Me.Label2.Text = "停止的服务"
        '
        'Label3
        '
        Me.Label3.Location = New System.Drawing.Point(8, 216)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(72, 23)
        Me.Label3.TabIndex = 8
        Me.Label3.Text = "暂停的服务"
        '
        'Button4
        '
        Me.Button4.Enabled = False
        Me.Button4.Location = New System.Drawing.Point(304, 328)
        Me.Button4.Name = "Button4"
        Me.Button4.TabIndex = 10
        Me.Button4.Text = "暂停服务"
        '
        'Button3
        '
        Me.Button3.Enabled = False
        Me.Button3.Location = New System.Drawing.Point(208, 328)
        Me.Button3.Name = "Button3"
        Me.Button3.TabIndex = 11
        Me.Button3.Text = "停止服务"
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(16, 328)
        Me.Button1.Name = "Button1"
        Me.Button1.TabIndex = 12
        Me.Button1.Text = "加载"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
        Me.ClientSize = New System.Drawing.Size(392, 365)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.ListBox3)
        Me.Controls.Add(Me.ListBox2)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.ListBox1)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label3)
        Me.Name = "Form1"
        Me.Text = "计算机服务管理"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Me.ServiceController1.MachineName = ComputerName
        Me.ServiceController1.DisplayName = DisplayName
        Try
            If Listcurrent.Equals(ListBox3) Then
                Me.ServiceController1.Continue()
                '当服务被暂停时,它必须继续
                '在这种情况下不可能启动
            ElseIf Listcurrent.Equals(ListBox2) Then
                Me.ServiceController1.Start()
            End If
            System.Threading.Thread.Sleep(5000)
            While ServiceController1.Status _
            = ServiceControllerStatus.StartPending
                Application.DoEvents()
            End While
            If Me.ServiceController1.Status = _
            ServiceControllerStatus.Running Then
                ListBox1.Items.Add(Listcurrent.SelectedItem.ToString())
                Listcurrent.Items.Remove(Listcurrent.SelectedItem.ToString())
                Listcurrent.Refresh()
                ListBox1.Refresh()
                '如果已经启动刷新ListBox1和启动的服务原来所在的列表框
            Else
                MessageBox.Show(ServiceController1.ServiceName + " 无法启动")
                '如果无法启动显示消息框
            End If
        Catch
            MessageBox.Show("服务: " + DisplayName + " 未能启动!")
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
        ComputerName = System.Environment.MachineName
        '采用本地计算机	
        If Not (ServiceController1 Is Nothing) Then
            Me.ListBox1.Items.Clear()
            Me.ListBox2.Items.Clear()
            Me.ListBox3.Items.Clear()
            ServiceController1 = Nothing
        End If
        '清理服务管理器 

        Dim ArraySrvCtrl() As ServiceController
        Dim tempSC As ServiceController
        Try
            ArraySrvCtrl = ServiceController.GetServices(ComputerName)
            '在ArraySrvCtrl数组中存储所有服务
            For Each tempSC In ArraySrvCtrl
                If tempSC.Status = ServiceControllerStatus.Running Then
                    ListBox1.Items.Add(tempSC.DisplayName)
                    '将正在运行的服务添加到ListBox1中
                Else
                    If tempSC.Status = ServiceControllerStatus.Paused Then
                        ListBox3.Items.Add(tempSC.DisplayName)
                        '将暂停的服务添加到ListBox3中
                    Else
                        ListBox2.Items.Add(tempSC.DisplayName)
                        '将停止的服务添加到ListBox2中
                    End If
                End If
            Next
        Catch
        End Try
        Me.Button4.Enabled = False
        Me.Button2.Enabled = False
        Me.Button3.Enabled = False
        Me.ServiceController1 = tempSC
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.ServiceController1.MachineName = ComputerName
        Me.ServiceController1.DisplayName = DisplayName

        Try
            If Me.ServiceController1.CanStop Then
                '如果当前服务可以停止
                Try
                    Me.ServiceController1.Stop()
                    '停止当前服务
                    System.Threading.Thread.Sleep(500)
                    '等待一段事件
                    While Me.ServiceController1.Status = ServiceControllerStatus.StopPending
                        Application.DoEvents()
                    End While
                    ListBox2.Items.Add(Listcurrent.SelectedItem.ToString())
                    Listcurrent.Items.Remove(Listcurrent.SelectedItem.ToString())
                    Listcurrent.Refresh()
                    ListBox2.Refresh()
                    '更新列表框
                Catch
                    MessageBox.Show("服务: " + Me.ServiceController1.DisplayName + " 不允许停止!")
                End Try
            Else
                MessageBox.Show("服务: " + Me.ServiceController1.DisplayName + " 不允许停止!")
            End If
        Catch
        End Try
        Me.Button4.Enabled = False
        Me.Button2.Enabled = False
        Me.Button3.Enabled = False

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.ServiceController1.MachineName = ComputerName
        Me.ServiceController1.DisplayName = DisplayName
        Try
            Me.ServiceController1.Pause()
            ListBox3.Items.Add(Listcurrent.SelectedItem.ToString())
            Listcurrent.Items.Remove(Listcurrent.SelectedItem.ToString())
            Listcurrent.Refresh()
            ListBox3.Refresh()
        Catch
            MessageBox.Show("服务: " + DisplayName + " 未能暂停!")
        End Try
        '并刷新列表框
        Me.Button4.Enabled = False
        Me.Button2.Enabled = False
        Me.Button3.Enabled = False

    End Sub

    Private Sub ListBox1_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles ListBox1.MouseUp
        Me.Listcurrent = ListBox1
        DisplayName = Listcurrent.SelectedItem.ToString()
        Me.Button3.Enabled = True
        Me.Button4.Enabled = True
        '当单击ListBox1中的列表项时
        '在Listcurrent中存储当前列表框
    End Sub

    Private Sub ListBox2_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles ListBox2.MouseUp
        Me.Listcurrent = ListBox2
        DisplayName = Listcurrent.SelectedItem.ToString()
        Me.Button2.Enabled = True
        Me.Button4.Enabled = True
    End Sub

    Private Sub ListBox3_MouseUp(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.MouseEventArgs) _
    Handles ListBox3.MouseUp
        Me.Listcurrent = ListBox3
        DisplayName = Listcurrent.SelectedItem.ToString()
        Me.Button2.Enabled = True
        Me.Button3.Enabled = True

    End Sub

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

    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -