使用visual basic 2010更改ip

时间:2014-11-30 21:53:12

标签: vb.net visual-studio-2010

我试图用visual basic改变我的ip 我是Visual Basic的新手,实际上我几乎没用它。 我有一个我需要的大学项目,但我无法完成它。 希望你们能在黑暗中展示一些亮光。

这是我的代码:

Imports System.Management
Imports System.Net
Imports System.Net.NetworkInformation
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim IPAddress As String = (TextBox1.Text)
        Dim SubnetMask As String = (TextBox2.Text)
        Dim Gateway As String = (TextBox3.Text)

        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)

                Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!")

            Catch ex As Exception
                MessageBox.Show("Unable to Set IP : " & ex.Message)
            End Try
        Next objMO
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            getip()
        Catch ex As Exception
            ex.ToString()
        End Try

        Dim PrimaryNic As New Collection
        Dim PNic As String = ""
        For Each networkCard As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces
            For Each gatewayAddr As GatewayIPAddressInformation In networkCard.GetIPProperties.GatewayAddresses
                If gatewayAddr.Address.ToString <> "0.0.0.0" And networkCard.OperationalStatus.ToString() = "Up" Then
                    Dim IpAddress As UnicastIPAddressInformation

                    For Each IpAddress In networkCard.GetIPProperties.UnicastAddresses
                        TextBox2.Text = (IpAddress.IPv4Mask.ToString)
                    Next
                    ' Get IP gateway information  
                    TextBox3.Text = (gatewayAddr.Address.ToString)
                End If
            Next
        Next

    End Sub

    Private Sub getip()
        Dim hostname As String = Dns.GetHostName
        Dim ip As String = System.Net.Dns.GetHostByName(hostname).AddressList(0).ToString

        TextBox1.Text = ip
    End Sub
End Class

正如您所看到的,我制作了3个文本框,从组件中检索静态IP,子网掩码和网关。 这样,如果计算机有其他数据而不是192.168,那么它也可以使用它。

但它不起作用,当我按下按钮时,它甚至没有想到。什么也没发生。

我也尝试从批处理文件中运行它,它也不起作用。它更改了ip(批处理文件中的特定文件,但互联网连接已停止工作)

我正在虚拟机(Windows 7)上测试它,如果出现问题,我不想搞错连接。 (我不知道在VM上测试它是否重要)

希望你能帮助我 非常感谢!

0 个答案:

没有答案