VB 修改 IP 地址 (100%可用)

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

4个text控件,2个command控件

Dim strComputer As String, IPAddress As String

Dim objWMIService As Object, IPConfigSet As Object, IPConfig, strAddress

Private Sub Command1_Click()

Call GetIP

MsgBox "获取成功"

End Sub

Private Sub Command2_Click()

Call SetIP

End Sub

Private Sub Command3_Click()

Label4(0).Visible = True

Label4(1).Visible = True

Label4(2).Visible = True

Label4(3).Visible = True

Label4(4).Visible = True

End Sub

Private Sub Form_Load()

Dim i As Integer

strComputer = "."

Const c = ","

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") ' Set IPConfigSet = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

Set IPConfigSet = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")

End Sub

Function GetIP() As String

Text1.Text = ""

Text2.Text = ""

Text3.Text = ""

Text4.Text = ""

For Each IPConfig In IPConfigSet

If Not IsNull(IPConfig.IPAddress) Then

For i = LBound(IPConfig.IPAddress) To UBound(IPConfig.IPAddress)

Text1.Text = Text1 + IPConfig.IPAddress(i)

Text2.Text = Text2 + IPConfig.IPSubnet(i)

If UBound(IPConfig.IPAddress) > i Then

Text1.Text = Text1.Text + c

Text2.Text = Text2.Text + c

End If

Next

For i = LBound(IPConfig.DefaultIPGateway) To UBound(IPConfig.DefaultIPGateway) Text3.Text = Text3.Text + IPConfig.DefaultIPGateway(i)

If UBound(IPConfig.DefaultIPGateway) > i Then

Text3.Text = Text3.Text + c

End If

Next

For i = LBound(IPConfig.DNSServerSearchOrder) To UBound(IPConfig.DNSServerSearchOrder)

Text4.Text = Text4.Text + IPConfig.DNSServerSearchOrder(i)

If UBound(IPConfig.DNSServerSearchOrder) > i Then

Text4.Text = Text4.Text + c

End If

Next

End If

Next

End Function

Function SetIP() As String

Dim IP, Subnetmask, Gateway, DNS

Dim ErrIP, ErrGateway, ErrDNS

IP = Array(Text1.Text)

Subnetmask = Array(Text2.Text)

Gateway = Array(Text3.Text)

DNS = Array(Text4.Text)

For Each IPConfig In IPConfigSet

ErrIP = IPConfig.EnableStatic(IP, Subnetmask)

ErrGateway = IPConfig.SetGateways(Gateway)

ErrDNS = IPConfig.SetDNSServerSearchOrder(DNS)

If ErrIP = 0 Then

MsgBox ("IP设置成功")

Else

MsgBox ("IP设置失败,请确认您的输入无误,并拥有相关权限") End If

Next

End Function

相关文档
最新文档