1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
|
Imports System
Imports System.Management
Imports System.Threading
Namespace WmiIpChanger
Class IpChanger
<MTAThread()> _
Shared Sub Main(ByVal args As String())
ReportIP
SwitchToStatic
Thread.Sleep(5000)
ReportIP
Console.WriteLine("end.")
Sub
Shared Sub SwitchToDHCP()
Dim inPar As ManagementBaseObject = Nothing
Dim outPar As ManagementBaseObject = Nothing
Dim mc As ManagementClass = New ManagementClass
("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances
For Each mo As ManagementObject In moc
If CType(mo("IPEnabled"), Boolean) Then
inPar = mo.GetMethodParameters("EnableDHCP")
outPar = mo.InvokeMethod("EnableDHCP", inPar, Nothing)
Exit For
End If
Next
End Sub
Shared Sub SwitchToStatic()
Dim inPar As ManagementBaseObject = Nothing
Dim outPar As ManagementBaseObject = Nothing
Dim mc As ManagementClass = New ManagementClass
("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances
For Each mo As ManagementObject In moc
If CType(mo("IPEnabled"), Boolean) Then
inPar = mo.GetMethodParameters("EnableStatic")
inPar("IPAddress") = New String() {"192.168.1.1"}
inPar("SubnetMask") = New String() {"255.255.255.0"}
outPar = mo.InvokeMethod("EnableStatic", inPar, Nothing)
Exit For
End If
Next
End Sub
Shared Sub ReportIP()
Console.WriteLine("****** Current IP addresses:")
Dim mc As ManagementClass = New ManagementClass
("Win32_NetworkAdapterConfiguration")
Dim moc As ManagementObjectCollection = mc.GetInstances
For Each mo As ManagementObject In moc
If CType(mo("IPEnabled"), Boolean) Then
Console.WriteLine("{0}" & Microsoft.VisualBasic.Chr(10) & " SVC:
'{1}' MAC: [{2}]", CType(mo("Caption"), String), CType(mo
("ServiceName"), String), CType(mo("MACAddress"), String))
Dim addresses As String() = CType(mo("IPAddress"), String())
Dim subnets As String() = CType(mo("IPSubnet"), String())
Console.WriteLine(" Addresses :")
For Each sad As String In addresses
Console.WriteLine("" & Microsoft.VisualBasic.Chr(9) & "'{0}'", sad)
Next
Console.WriteLine(" Subnets :")
For Each sub As String In subnets
Console.WriteLine("" & Microsoft.VisualBasic.Chr(9) & "'{0}'", sub)
Next
End If
Next
End Sub
End Class
End Namespace |
Partager