VB.NET不但可以帮助开发人员实现各种硬件设备信息的产看等功能,还能通过它对网络进行监控。在这里我们就会为大家详细介绍有关VB.NET网络监控的相关操作方法,希望对初学者们有所帮助。#t#
VB.NET网络监控的开发环境:Visual Studio.net 2005
1.在窗体上放置一个TreeView控件,改名为tvInfo。
2.再放置一个按钮控件。
3.双击按钮,编写VB.NET网络监控代码。
- Dim _ManagementObjectSearcher As
New System.Management.Management
ObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration") - Dim _ParenNode As TreeNode =
Nothing '父节点 - Me.tvInfo.Nodes.Clear()
- Try
- For Each ManObj As System.
Management.ManagementObject
In _ManagementObjectSearcher.Get - _ParenNode = New TreeNode
(ManObj("Description")) - If CBool(ManObj("IPEnabled")) Then
创建 ip地址及子网掩码 子节点
- Dim _IpNode As New TreeNode
("IP Address | Default Gateway")- If (ManObj("IPAddress")
IsNot Nothing) Then- For i As Integer = 0 To
UBound(ManObj("IPAddress"))- _IpNode.Nodes.Add(New TreeNode
(ManObj("IPAddress")(i) & "
| " & ManObj("IPSubnet")(i)))- Next
- Else
- _IpNode.Nodes.Add(New
TreeNode("Null"))- End If
创建 默认网关 子节点
- Dim _GatewayNode As New
TreeNode("Default Gateway")- If (ManObj("DefaultIPGateway")
IsNot Nothing) Then- For i As Integer = 0 To UBound
(ManObj("DefaultIPGateway"))- _GatewayNode.Nodes.Add(New
TreeNode(ManObj("DefaultIP
Gateway")(i)))- Next
- Else
- _GatewayNode.Nodes.Add(New
TreeNode("Null"))- End If
创建 DNS服务器地址 子节点
- Dim _DNSNode As New TreeNode
("DNS Servers")- If (ManObj("DNSServerSearchOrder")
IsNot Nothing) Then- For i As Integer = 0 To UBound
(ManObj("DNSServerSearchOrder"))- _DNSNode.Nodes.Add(New TreeNode
(ManObj("DNSServerSearchOrder")(i)))- Next
- Else
- _DNSNode.Nodes.Add(New TreeNode("Null"))
- End If
- _ParenNode.Nodes.AddRange( _
- New TreeNode() { _
- New TreeNode(String.Format
("Physical Address:{0}", ManObj
("MACAddress"))), _- New TreeNode(String.Format
("DHCPEnabled:{0}", ManObj
("DHCPEnabled"))), _- _IpNode, _
- _GatewayNode, _
- _DNSNode _
- })
- End If
- _ParenNode.ExpandAll()
- Me.tvInfo.Nodes.Add
(_ParenNode)- Next
- Catch ex As Exception
- MsgBox(ex.Message,
MsgBoxStyle.Exclamation)- End Try
VB.NET网络监控的实际操作步骤就为大家介绍到这里。