在VB.Net中更改IP地址 [英] Change IP Address in VB.Net

查看:315
本文介绍了在VB.Net中更改IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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版本)和原始代码为此处(C#版) 。这个解决方案使用的WMI我真的不太了解。

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.

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

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?

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

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


推荐答案

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

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.

这篇关于在VB.Net中更改IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆