VB.NET编程语言主要是面向对象。随着它的应用日趋成熟,大家慢慢体会到了其功能的强大和新颖。在这里我们会通过一个VB.NET设置IP的小例子来详细分析这一语言的具体用法,希望能给大家带来帮助。
这些天总是带着笔记本在家/单位/客户等等几个地方跑来跑去的,于是在各个地方上网都是要重新设置的,有时候有线,有时候无线。唉,昨天我终于爆发了,再也受不了这么没有尽头的折磨了。于是我又用VB写了一个简单的IP设置转换器。
VB.NET设置IP其实利用VB修改ip地址是比较容易的。我利用的就是wmi方式。先是找出当前系统的所有网卡信息,下面给出的是找出所有网卡MAC地址的例程: Function GetMACaddress()
Dim tempBool As Boolean strComputer = "."
Set objWMIServiceGL = GetObject
("winmgmts:\\" & strComputer &
"\root\cimv2")
Set colNetAdaptersGL =
objWMIServiceGL.ExecQuery
("Select * from Win32_Network
AdapterConfiguration where
(IPEnabled=TRUE)")
For Each obj In objs
getMACAddressgetMACAddress =
getMACAddress & obj.macaddress
& VBCrLf & VBCrLf
'Exit For '找第一个网卡就退出
Next obj End Function
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
VB.NET设置IP大下一步就是根据所找到的各个网卡的进行信息(IP,DNS等)更改:
Function ModifyIP() strComputer = "."
Set objWMIService = GetObject
("winmgmts:\\" & strComputer &
"\root\cimv2")
Set objSWbemObjectSet = objSWbem
Services.ExecQuery("Select * From
Win32_NetworkAdapterConfiguration
Where Description='" &
Combo1.Text & "'")
For Each objNetAdapter In colNet
Adapters sip = objNetAdapter.
IPaddress(0)
If Option1.Value = True Then
'DHCP is enabled
objNetAdapter.EnableDHCP
errDNS = objNetAdapter.SetDNS
ServerSearchOrder()
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.SetDNS
ServerSearchOrder(StrDns)
'Exit For '只修改第一个网卡的设置
End If Next
End Function
- 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.
VB.NET设置IP的相关技巧就为大家介绍到这里。
【编辑推荐】