在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
最满意答案
我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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 ...
-
如何在VB.NET中显示客户端在服务器端的IP地址?(How to show the client's IP address in server side in VB.NET?)[2023-03-30]
在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 ...
-
在vb.net中获取客户端IP(Get client IP in vb.net)[2022-07-24]
如果您使用的是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 ...
-
2个不同的IP地址(2 Different IP Address)[2022-12-06]
显然,你是一些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 ... -
VB.Net返回IPv4地址(VB.Net Return IPv4 Address)[2022-06-19]
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 ...
-
在VB.Net中更改IP地址(Change IP Address in VB.Net)[2022-09-12]
我可以回答我自己的问题。 我在洗澡时想到了它(陈词滥调怎么样?)。 因为这是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 ...