轻松掌握VB.NET设置IP技巧

开发 后端
VB.NET设置IP的实现比较容易,可以利用wmi方式来进行具体的操作。我们可以通过本文介绍的相关方法对此有一个详细的解读。

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的相关技巧就为大家介绍到这里。

【编辑推荐】

  1. 几步解决VB.NET编写病毒
  2. VB.NET特殊操作符总结概述
  3. VB.NET操作符like作用体现
  4. VB.NET运算符代码示例解析
  5. VB.NET文本框应用经验总结
责任编辑:曹凯 来源: 中国IT实验室
相关推荐

2010-01-18 19:36:52

VB.NET调整控件

2010-01-13 17:47:59

VB.NET拖放

2010-01-08 18:16:52

VB.NET变量

2010-01-14 10:07:08

VB.NET文件名排序

2010-01-15 13:52:42

VB.NET属性设置

2010-01-11 13:33:07

VB.NET使用数组

2010-01-18 10:13:08

VB.NET获取CPU

2010-01-11 16:04:10

VB.NET使用wit

2009-10-29 09:06:26

VB.NET Web

2010-01-13 18:28:21

VB.NET历史菜单

2010-01-11 15:12:30

VB.NET特殊窗体

2010-01-12 10:19:02

VB.NET操作GDI

2009-10-29 15:02:04

VB.NET文件排序

2009-11-03 09:37:33

VB.NET重载

2009-10-12 15:44:26

VB.NET动态编码技

2010-01-18 18:20:49

VB.NET使用API

2009-11-10 13:08:13

VB.NET编程技巧

2010-01-11 14:28:14

VB.NET操作Exc

2009-10-29 13:38:05

VB.NET Shar

2009-10-30 14:45:42

Flash控制VB.N
点赞
收藏

51CTO技术栈公众号