在 VB.Net 中更改 IP 地址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14777466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:16:04  来源:igfitidea点击:

Change IP Address in VB.Net

vb.netipwmiip-address

提问by Thomas927

I am writing a Windows Forms App in VB.Net that will (among other things) change the IP Address, Default Gateway, Subnet Mask, and set the IP Address to Static on an image of Winfows 7 only. Sysprep is not being used. I have searched Google and only come up with 2 options. I don't believe the first solution will work for me because I do not necessarily know the name of the connection. It uses netsh to change IP settings. I was going to give a link to this example but I can't post more than 2 links...

我正在 VB.Net 中编写一个 Windows 窗体应用程序,它将(除其他外)更改 IP 地址、默认网关、子网掩码,并将 IP 地址设置为仅在 Winfows 7 的图像上静态。未使用 Sysprep。我在谷歌上搜索过,只找到了 2 个选项。我不相信第一个解决方案对我有用,因为我不一定知道连接的名称。它使用 netsh 来更改 IP 设置。我打算提供一个链接到这个例子,但我不能发布超过 2 个链接......

The second solution is shown at this link (the VB.Net version)and the original code is here (the C# version). This solution uses WMI which I really don't know that much about.

第二种解决方案显示在此链接(VB.Net 版本),原始代码在这里(C# 版本)。这个解决方案使用了我真的不太了解的 WMI。

When I debug the code and look at everything the code seems to be executing properly but the IP Address is still set to DHCP and all of the other setting are still the same. So, basically, what gives? Why does this code seem to not work?

当我调试代码并查看所有代码时,代码似乎正常执行,但 IP 地址仍设置为 DHCP,所有其他设置仍然相同。那么,基本上,是什么给出了?为什么这段代码似乎不起作用?

Here is my code. I only made a few changes:

这是我的代码。我只做了一些改变:

    'Changed the 3 IPs below
    Dim IPAddress As String = "192.168.1.105"
    Dim SubnetMask As String = "255.255.252.0"
    Dim Gateway As String = "192.168.1.100"

    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)

            'Changed this line so I could see if it was executing all of the way
            MessageBox.Show("Updated IPAddress, SubnetMask and Default Gateway!")

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

采纳答案by Thomas927

I can answer my own question. I thought of it in the shower (how cliche right?). Because this is Windows 7, all I needed to was right-click and run the program as an administrator.

我可以回答我自己的问题。我在淋浴时想到了它(多么陈词滥调?)。因为这是 Windows 7,所以我只需要右键单击并以管理员身份运行该程序。