Vb.net 如何使用这些代码更改IP地址?

Vb.net 如何使用这些代码更改IP地址?,vb.net,static-ip-address,Vb.net,Static Ip Address,我正在使用此代码更改Ip地址、子网掩码和默认网关,但似乎只有默认网关更改,而Ip地址和子网掩码没有更改。你能帮帮我吗?我正在使用Windows7 Dim IPAddress As String = "192.168.2.130" Dim SubnetMask As String = "255.0.0.0" Dim Gateway As String = "192.168.2.1" Dim objMC As ManagementClass = New ManagementClass("Win32_

我正在使用此代码更改Ip地址、子网掩码和默认网关,但似乎只有默认网关更改,而Ip地址和子网掩码没有更改。你能帮帮我吗?我正在使用Windows7

Dim IPAddress As String = "192.168.2.130"
Dim SubnetMask As String = "255.0.0.0"
Dim Gateway As String = "192.168.2.1"
Dim objMC As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()

For Each objMO As ManagementObject In objMOC
    If (Not CBool(objMO("IPEnabled"))) Then
        Continue For
    End If

    Try
        Dim objNewIP As ManagementBaseObject = Nothing
        Dim objSetIP As ManagementBaseObject = Nothing
        Dim objNewGate As ManagementBaseObject = Nothing
        objNewIP = objMO.GetMethodParameters("EnableStatic")
        objNewGate = objMO.GetMethodParameters("SetGateways")

        'Set DefaultGateway
        objNewGate("DefaultIPGateway") = New String() {Gateway}
        objNewGate("GatewayCostMetric") = New Integer() {1}

        'Set IPAddress and Subnet Mask
        objNewIP("IPAddress") = New String() {IPAddress}
        objNewIP("SubnetMask") = New String() {SubnetMask}
        objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, Nothing)
        objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, Nothing)

        MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")
    Catch ex As Exception
        MessageBox.Show("Unable to Set IP : " & ex.Message)
    End Try
Next objMO

右键单击bin中的exe并以管理员身份运行,因为更改IP地址需要管理员权限,而在Windows 7中,要获得此类权限,您必须以管理员身份运行它。

作为注释会更好。如果你想回答这个问题,最好解释一下为什么以管理员身份运行exe可以解决这个问题。谢谢@Tim。我用我的ipad回答,这意味着我能写的字符越少越好。我正在根据你的要求编辑我的答案。再次感谢,没问题。感谢您对答案进行编辑并添加更多内容。我只是那些总是想知道事情发生的原因的人中的一员……我很确定我小时候把父母逼疯了:)加上OP以外的人将来可能需要这个答案,了解更多细节几乎总是有帮助的。