首页 \ 问答 \ 在VB.Net中更改IP地址(Change IP Address in VB.Net)

在VB.Net中更改IP地址(Change IP Address in VB.Net)

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

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

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

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

    '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

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...

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.

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
更新时间:2022-09-12 16:09

最满意答案

我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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.

相关问答

更多
  • 每个用户都与IP相关,并且只允许与此连接。 可能您的用户'测试'被允许从'IP'连接。 并且'IP'不一定转换为'127.0.0.1',即使它是同一台机器。 例如,使用mysql命令行add(或类似于phpmyadmin): create user 'test'@'127.0.0.1' identified by 'your_password'; Each user is related with an IP, and allowed to connect only from this one. Prob ...
  • 在AcceptStart , handler是Socket类型,包含您需要的内容。 你可以做 Dim ip As IPEndPoint = handler.RemoteEndPoint TextMsg.Text = TextMsg.Text + ip.Address + "on port " + ip.Port + Environment.NewLine In AcceptStart, handler is of the Socket type and that contains what you nee ...
  • 遗憾的是,Internet Explorer是win表单中Web浏览器组件的基础。 反过来,默认情况下它不会理解MJPEG文件格式,因此会要求您下载该文件。 尝试使用铬(开源铬),因为它应该处理这种文件格式。 https://thechriskent.com/2014/08/18/embedded-chromium-in-winforms/ 或者你可以看看这里的选项: 用于显示MJPEG流的跨浏览器解决方案,因为你可以用另一种方式包装它 Internet Explorer is unfortunately ...
  • 如果您使用的是asp.net,则可以使用: Request.UserHostAddress 获取您的客户端IP地址 但如果您使用的是Windows应用程序获取本地IP地址,实际上您可能会收到多个IP地址! 如果你想得到一个你知道IP地址的netid部分的特定地址,你可以将所有IP地址与你知道的netid地址进行比较! if you are using asp.net you can use : Request.UserHostAddress to get your client ip address but ...
  • 由于IP4地址由4个字节组成,因此您只需使用大小为4的数组的Random.NextBytes即可创建一个。 要防止创建重复的地址,只需使用HashSet和while循环。 要过滤掉保留地址,您只需使用本答案中使用的代码(一些保留地址很容易检查,例如多播地址都以1110开头,但不需要在这里重新发明轮子)。 我将其转换为VB.Net 所以你的代码看起来像这样: Dim r = New Random() Dim addresses = New HashSet(Of String)() While addres ...
  • 显然,你是一些NAT的背后。 因此,通过运行第一个代码,您将收到内部网络地址,第二个代码将为您提供网络可以访问Internet的真实(外部)IP地址。 这是因为第二种方法只是调用外部网站来确定您的IP,并且该网站只能确定真实IP地址,而不是内部IP地址。 Obviously, you are behind some NAT. So by running first code you're receiving your internal network address, and second code giv ...
  • Dim myClientMachineAddressList As IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()) Dim myClientMachineIP As String = myClientMachineAddressList.AddressList(0).ToString() 编辑: 然后你可以使用IPAddress.AddressFamily来找出IP familly类型。 Dim myClien ...
  • 您可以通过这种方式获取客户端的IP: Dim IP = "" If (Request.Properties.ContainsKey("MS_HttpContext")) Then IP = DirectCast(Request.Properties("MS_HttpContext"), HttpContextWrapper) _ .Request.UserHostAddress ElseIf (Request.Properties.ContainsKey(RemoteEndp ...
  • 我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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地址。 我有我自己的一个: http://etoys.netortech.com/devpages/ip.asp 虽然我不能保证它永远在那里,这就是为什么你应该自己做。 从那里使用HttpWebRequest来下载页面内容是一件简单的事情。 There's no way to do ...

相关文章

更多

最新问答

更多
  • 在javascript中创建类以创建对象并在Java中创建类和对象之间的区别(Difference between creating a class in javascript to create an object and creating an class and object in Java)
  • Facebook API:将身份验证详细信息从Javascript SDK发送到PHP SDK(Facebook API: Send authentication detail from Javascript SDK to PHP SDK)
  • 如何停止队列动画jquery?(How can I stop queue animation jquery?)
  • 使用C#的井字游戏中的人工智能(Artificial Intelligence in Tic-Tac-Toe using C#)
  • 多少流量可以共享虚拟主机(对于Python Django站点)支持?(How Much Traffic Can Shared Web Hosting (for a Python Django site) support?)
  • 带有CIFilters的CAShapeLayer(CAShapeLayer with CIFilters)
  • 如何在Angular 2中读取JSON #text(How to read in Angular 2 the JSON #text)
  • 如何在xml中读取自闭标签的属性?(How to read self closing tag's attribute in xml?)
  • 无法使用http put将图像上传到亚马逊S3(Cannot upload image to amazon s3 using http put)
  • 文件结束无限循环(end of file infinite while-loop)
  • 在cpp的模板(template in cpp)
  • 在构建库时,clang和clang ++有什么区别?(What's the difference between clang and clang++ when building a library?)
  • ng类中的表达式(expression inside ng-class)
  • 在PHP中获取随机布尔值true / false(Get random boolean true/false in PHP)
  • 管道的高效分块用于严格的字节串(Efficient chunking of conduit for strict bytestring)
  • Python ternary_operator(如果其他标志做了其他操作,则执行其他操作)(Python ternary_operator (do-somthing if flag else do-another))
  • Sencha Touch面具发布(Sencha Touch mask ondisclosure)
  • 验证脚本上的通知[重复](Notices on validation script [duplicate])
  • 朋友功能(friend function)
  • 基于角坐标平移和变换平面几何(Translate and transform plane geometry based on corner coordinates)
  • Rails:'如果在本地运行'条件javascript标记包括(Rails: 'if running locally' conditional javascript tag include)
  • 解压文件(Unzipping files)
  • 使用ui-router以角度加载变量状态(loading in variable states with ui-router in angular)
  • 创建Azure云服务需要多长时间?(how long does it take to create an Azure Cloud Service? How to view log information?)
  • 指向整数的指针数组(Array of pointers to integers)
  • Laravel服务提供商没有看到我的包的主要类(Laravel service provider does not see the main class of my package)
  • 这个关于VSS / RSS / PSS / USS的解释是否准确?(Is this explanation about VSS/RSS/PSS/USS accurate?)
  • 在Django-Admin中通过row-id排序显示项目(Ordering the display items by row-id in Django-Admin)
  • 如何使用cythonize启用`--embed`?(How to enable `--embed` with cythonize?)
  • 用于将文本多行设置的Excel脚本(Excel script for ereasing text multiple rows)