Imports System.Collections.Generic Imports System.ComponentModel Imports System.Data Imports System.Drawing Imports System.Linq Imports System.Text Imports System.Threading.Tasks Imports System.Windows.Forms Imports System.Management Imports System.Net.NetworkInformation Imports System.Net.Sockets Imports System.Threading Public Class NetworkHelp Public Shared Function SetNetworkAdapter() As Boolean Dim inPar As ManagementBaseObject = Nothing Dim outPar As ManagementBaseObject = Nothing Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration") Dim moc As ManagementObjectCollection = mc.GetInstances() Dim isNetworkInterfaces As NetworkInterface() = System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces() Dim ipstr As String = String.Empty Dim ipProperties As IPInterfaceProperties Dim gateways As UnicastIPAddressInformationCollection For Each inten In isNetworkInterfaces If inten.NetworkInterfaceType = NetworkInterfaceType.Ethernet Then ipProperties = inten.GetIPProperties gateways = ipProperties.UnicastAddresses For Each tmp In gateways If tmp.Address.AddressFamily = AddressFamily.InterNetwork Then ipstr = tmp.Address.ToString End If Next End If Next If String.IsNullOrEmpty(ipstr) Then MsgBox("以太网连接中断") Return False End If Dim ipaddstr() As String Dim isflag As Boolean = True For Each mo As ManagementObject In moc ipaddstr = mo("IPAddress") If IsNothing(ipaddstr) Then Continue For For Each cipstr In ipaddstr If cipstr.Equals(ipstr) Then isflag = False End If Next If isflag Then Continue For 'If Not CBool(mo("IPEnabled")) And isflag Then Continue For inPar = mo.GetMethodParameters("EnableStatic") inPar("IPAddress") = New String() {"192.168.1.10"} inPar("SubnetMask") = New String() {"255.255.0.0"} outPar = mo.InvokeMethod("EnableStatic", inPar, Nothing) inPar = mo.GetMethodParameters("SetGateways") inPar("DefaultIPGateway") = New String() {"192.168.1.1"} outPar = mo.InvokeMethod("SetGateways", inPar, Nothing) inPar = mo.GetMethodParameters("SetDNSServerSearchOrder") inPar("DNSServerSearchOrder") = New String() {"114.114.114.114", "223.5.5.5"} outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, Nothing) Return True Exit For Next Thread.Sleep(100) Return False End Function Public Shared Sub SetNetworkAdapterDHCP() Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration") Dim moc As ManagementObjectCollection = mc.GetInstances() For Each mo As ManagementObject In moc If Not CBool(mo("IPEnabled")) Then Continue For mo.InvokeMethod("SetDNSServerSearchOrder", Nothing) mo.InvokeMethod("EnableStatic", Nothing) mo.InvokeMethod("SetGateways", Nothing) mo.InvokeMethod("EnableDHCP", Nothing) Exit For Next End Sub End Class