計算機二級考試VisualBasic輔導:vb簡單的IP設置轉換器

字號:

其實利用vb修改ip地址是比較容易的。我利用的就是wmi方式。先是找出當前系統(tǒng)的所有網卡信息,下面給出的是找出所有網卡MAC地址的例程:
    Function GetMACaddress() Dim tempBool As Boolean strComputer = "."
    Set objWMIServiceGL = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") Set colNetAdaptersGL = objWMIServiceGL.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where(IPEnabled=TRUE)")
    For Each obj In objs
    getMACAddress = getMACAddress & obj.macaddress & vbCrLf & vbCrLf
    'Exit For '找第一個網卡就退出
    Next obj End Function
    然后根據(jù)所找到的各個網卡的進行信息(IP,DNS等)更改:
    Function ModifyIP() strComputer = "." Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set objSWbemObjectSet = objSWbemServices.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where Description='" & Combo1.Text & "'")
    For Each objNetAdapter In colNetAdapters sip = objNetAdapter.IPaddress(0)
    If Option1.Value = True Then 'DHCP is enabled
    objNetAdapter.EnableDHCP
    errDNS = objNetAdapter.SetDNSServerSearchOrder()
    Else
    strIPAddress = Array(Text1.Text)
    strSubnetMask = Array(Text2.Text)
    strGateway = Array(Text3.Text)
    strGatewaymetric = Array(1)
    StrDns = Array(Text4.Text, Text5.Text)
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    errDNS = objNetAdapter.SetDNSServerSearchOrder(StrDns)
    'Exit For '只修改第一個網卡的設置
    End If Next End Function