[VB.net]网络状态监视器[2012.02.28]

前端之家收集整理的这篇文章主要介绍了[VB.net]网络状态监视器[2012.02.28]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

程序可以监控网络连接状态,可以设定网络断开的话就自动关机或运行程序。

新浪博客http://blog.sina.com.cn/s/blog_647ee5f70100wicy.html


更新
2.细节调整
3.修复Bug
4.错误处理



界面图及控件命名如下:


此外,程序还包含两个Timer控件,Timer1用于定时执行命令,Timer2用于获取当前时间并显示;一个NotifyIcon控件用于显示托盘图标;一个ContextMenuStrip控件用于托盘图标的右键菜单;一个OpenFileDialog控件用于打开文件


代码

Public Class Monitor1

    Dim a As Boolean

    Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load
        Label3.Text = "当前时间:" & My.Computer.Clock.LocalTime.ToLongTimeString   '获取当前时间并显示
        Timer1.Interval = My.Settings.Interval1    '从设置中获取保存的检测间隔
        ComboBox1.SelectedIndex = 1               '令检测间隔选择框默认显示第二项

        If My.Settings.LWS = 1 Then    '初始化【开机启动】复选框状态
            CheckBox3.Checked = True
        Else
            CheckBox3.Checked = False
        End If

        If My.Settings.SWR = 1 Then   '获取【运行即监控】设置
            Timer1.Enabled = True
            CheckBox4.Checked = True
            Button1.Text = "关闭监视"
            Label2.Text = "监视功能状态:已开启"
            Label2.ForeColor = Color.DarkGreen
        Else
            Timer1.Enabled = False
            CheckBox4.Checked = False
            Button1.Text = "开启监视"
            Label2.Text = "监视功能状态:已关闭"
            Label2.ForeColor = Color.DarkRed
        End If

        a = My.Computer.Network.Ping("8.8.8.8")     '通过Ping 8.8.8.8来判断网络是否连通
        If a = True Then
            Label1.Text = "网络状态:网络已连接"       '如果网络已连接,则显示
            Light1.FillColor = Color.Green      '同时将“指示灯”颜色更改为绿色
            Light1.FillGradientColor = Color.LightGreen
            NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络已连接"
        Else
            Label1.Text = "网络状态:网络未连接"
            NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络未连接"
        End If
        NotifyIcon1.ContextMenuStrip = ContextMenuStrip1     '为托盘图标添加右键菜单
        If My.Settings.FilePath1 = "" Then
            TextBox1.Text = "请输入要运行的程序路径"
        Else
            TextBox1.Text = My.Settings.FilePath1
        End If
    End Sub

    Private Sub Timer1_Tick(sender As System.Object,_
                            e As System.EventArgs) Handles Timer1.Tick      'Timer1控件的Tick间隔为20000毫秒
        Dim result2 As String

        a = My.Computer.Network.Ping("8.8.8.8")
        If a = True Then
            Label1.Text = "网络状态:网络已连接"
            Light1.FillColor = Color.Green
            Light1.FillGradientColor = Color.LightGreen
            NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络已连接"     '设置托盘图标的显示内容
        Else
            Label1.Text = "网络状态:网络未连接"      '如果网络未连接,则显示
            Light1.FillColor = Color.Red        '同时更改“指示灯”颜色为红色
            Light1.FillGradientColor = Color.Pink
            NotifyIcon1.Text = "网络监视器" & Chr(13) & "网络状态:网络未连接"
            If CheckBox1.Checked = True Then    '如果勾选【网络断开自动关机】
                Dim systemD As String
                systemD = System.Environment.SystemDirectory
                Process.Start(systemD & "\shutdown.exe","-s -t 60")    '则执行关机命令,延迟60秒
                Timer1.Enabled = False      '令Timer1不可用,以阻止多次执行同一动作
                result2 = MsgBox("网络已断开,计算机将在60秒内关闭",MsgBoxStyle.OkCancel)  '弹出对话框提示计算机即将关闭
                Me.ShowInTaskbar = True
                If result2 = vbCancel Then
                    Process.Start(systemD & "\shutdown.exe","-a")   '若用户在关机前点击对话框的“取消”按钮,则取消自动关机
                    CheckBox1.Checked = False
                    Timer1.Enabled = True
                End If
            Else
                If CheckBox2.Checked = True And TextBox1.Text <> "" Then    '如果勾选【网络断开运行程序】
                    Try
                        Process.Start(TextBox1.Text)    '则运行程序
                        CheckBox2.Checked = False
                    Catch ex As Exception
                        MsgBox("发生错误,可能是文件不存在!",MsgBoxStyle.Exclamation)
                    End Try
                End If
            End If
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object,e As System.EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            CheckBox2.Enabled = False
        Else
            CheckBox2.Enabled = True
        End If

    End Sub

    Private Sub CheckBox2_CheckedChanged(sender As System.Object,e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If CheckBox2.Checked = True Then   '如果选择了第二个单选按钮
            CheckBox1.Enabled = False     '禁用另一功能
            TextBox1.Enabled = True      '启用文本框
            If TextBox1.Text = "请输入要运行的程序路径" Then
                TextBox1.Text = ""           '清空文本框内容
            End If
            Button2.Enabled = True       '启用【浏览】按钮
            Button3.Enabled = True        '启用【保存路径】按钮
        Else
            CheckBox1.Enabled = True
            TextBox1.Enabled = False
            If TextBox1.Text = "" Then
                TextBox1.Text = "请输入要运行的程序路径"
            End If
            Button2.Enabled = False
            Button3.Enabled = False
        End If
    End Sub

    Private Sub CheckBox3_CheckedChanged(sender As System.Object,e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If CheckBox3.Checked = True Then
            Try
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",_
                                              "NetworkMonitor",Application.ExecutablePath)    '写入注册表
            Catch ex As Exception
                MsgBox("发生未知错误错误代码:" & Err.Number)    '其实几乎不可能发生错误
            End Try
            MsgBox("设置完毕,已启用开机自动运行",MsgBoxStyle.Information)   '提示
            My.Settings.LWS = 1    '保存设置
        Else
            Try
                My.Computer.Registry.SetValue("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run","")
            Catch ex As Exception
                MsgBox("发生错误错误代码:" & Err.Number,MsgBoxStyle.Exclamation)
            End Try
            MsgBox("设置完毕,已禁止开机自动运行",MsgBoxStyle.Information)
            My.Settings.LWS = 0
        End If
    End Sub

    Private Sub CheckBox4_CheckedChanged(sender As System.Object,e As System.EventArgs) Handles CheckBox4.CheckedChanged
        If CheckBox4.Checked = True Then
            My.Settings.SWR = 1    '保存设置
        Else
            My.Settings.SWR = 0
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object,e As System.EventArgs) Handles Button1.Click
        If Button1.Text = "关闭监视" Then    '点击【关闭监视】按钮
            Timer1.Enabled = False        '则禁用计时器,即停止监视功能
            Label2.Text = "监视功能状态:已关闭"     '显示状态
            Label2.ForeColor = Color.DarkRed          '修改字体颜色
            Button1.Text = "开启监视"               '修改按钮文本
        Else
            Timer1.Enabled = True
            Label2.Text = "监视功能状态:已开启"
            Label2.ForeColor = Color.DarkGreen
            Button1.Text = "关闭监视"
        End If
    End Sub

    Private Sub Button2_Click(sender As System.Object,e As System.EventArgs) Handles Button2.Click
        Try
            OpenFileDialog1.ShowDialog()
            TextBox1.Text = OpenFileDialog1.FileName
        Catch ex As Exception
            MsgBox("发生错误",MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub Button3_Click(sender As System.Object,e As System.EventArgs) Handles Button3.Click
        If TextBox1.Text <> "请请输入要运行的程序路径" And TextBox1.Text <> "" Then
            My.Settings.FilePath1 = TextBox1.Text
        Else
            MsgBox("请输入正确的路径",MsgBoxStyle.Exclamation)
        End If
    End Sub

    Private Sub Button4_Click(sender As System.Object,e As System.EventArgs) Handles Button4.Click
        If Me.Width = 425 Then
            Me.Width = 700    '展开【设置】部分
            GroupBox2.Enabled = True   '启用【设置】部分
            Button4.Text = "设置 <<"
        Else
            Me.Width = 425
            GroupBox2.Enabled = False
            Button4.Text = "设置 >>"
        End If
    End Sub

    Private Sub Button5_Click(sender As System.Object,e As System.EventArgs) Handles Button5.Click
        Dim interval2 As Integer
        If Button5.Text = "设置检测间隔" Then
            Button5.Text = "保存"
            ComboBox1.Enabled = True
        Else
            Select Case ComboBox1.Text
                Case "10秒"
                    interval2 = 10000
                Case "20秒(默认)"
                    interval2 = 20000
                Case "30秒"
                    interval2 = 30000
                Case "1分钟"
                    interval2 = 60000
                Case "2分钟"
                    interval2 = 120000
                Case "5分钟"
                    interval2 = 300000
                Case "10分钟"
                    interval2 = 600000
                Case "20分钟"
                    interval2 = 1200000
                Case "30分钟"
                    interval2 = 1800000
            End Select
            My.Settings.Interval1 = interval2
            Button5.Text = "设置检测间隔"
            ComboBox1.Enabled = False
        End If
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(sender As System.Object,e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.WindowState = FormWindowState.Normal    '双击托盘图标,显示窗口
        If Me.WindowState = FormWindowState.Minimized Then
            Me.ShowInTaskbar = True
        End If
    End Sub

    Private Sub monitor1_Resize(sender As Object,e As System.EventArgs) Handles Me.Resize
        If Me.WindowState = FormWindowState.Minimized Then
            Me.ShowInTaskbar = False   '当程序最小化,则不在任务栏显示图标
        End If
    End Sub

    Private Sub ShownHide1_Click(sender As System.Object,e As System.EventArgs) Handles ShownHide1.Click
        If ShownHide1.Text = "隐藏窗口" Then
            Me.WindowState = FormWindowState.Minimized
            ShownHide1.Text = "显示窗口"
        Else
            Me.WindowState = FormWindowState.Normal
            ShownHide1.Text = "隐藏窗口"
        End If
    End Sub

    Private Sub Exit1_Click(sender As System.Object,e As System.EventArgs) Handles Exit1.Click
        End
    End Sub

    Private Sub Timer2_Tick(sender As System.Object,e As System.EventArgs) Handles Timer2.Tick
        Label3.Text = "当前时间:" & My.Computer.Clock.LocalTime.ToLongTimeString
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As System.Object,e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        Try
            System.Diagnostics.Process.Start("mailto:wangjingkaibear@live.cn")
        Catch ex As Exception
            MsgBox("没有关联的邮件程序",MsgBoxStyle.Exclamation)
        End Try
    End Sub

    Private Sub OpenFileDialog1_FileOk(sender As System.Object,e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk

    End Sub
End Class
原文链接:https://www.f2er.com/vb/260243.html

猜你在找的VB相关文章