使用 vb.net 更改 dns 地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15427269/
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
change dns address using vb.net
提问by Vpp Man


how to change this two values in visual basi.net. I do not want to use vbscript to do this.
如何在visual basi.net 中更改这两个值。我不想使用 vbscript 来做到这一点。
i searched and get this - How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#
我搜索并得到了这个 -如何使用 C# 中的代码更改网络设置(IP 地址、DNS、WINS、主机名)
^ Converted code:
^ 转换后的代码:
''' <summary>
''' Set's a new IP Address and it's Submask of the local machine
''' </summary>
''' <param name="ip_address">The IP Address</param>
''' <param name="subnet_mask">The Submask IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setIP(ByVal ip_address As String, ByVal subnet_mask As String)
Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If CBool(objMO("IPEnabled")) Then
Try
Dim setIP As ManagementBaseObject
Dim newIP As ManagementBaseObject = objMO.GetMethodParameters("EnableStatic")
newIP("IPAddress") = New String() {ip_address}
newIP("SubnetMask") = New String() {subnet_mask}
setIP = objMO.InvokeMethod("EnableStatic", newIP, Nothing)
Catch generatedExceptionName As Exception
Throw
End Try
End If
Next
End Sub
''' <summary>
''' Set's a new Gateway address of the local machine
''' </summary>
''' <param name="gateway">The Gateway IP Address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setGateway(ByVal gateway As String)
Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If CBool(objMO("IPEnabled")) Then
Try
Dim setGateway As ManagementBaseObject
Dim newGateway As ManagementBaseObject = objMO.GetMethodParameters("SetGateways")
newGateway("DefaultIPGateway") = New String() {gateway}
newGateway("GatewayCostMetric") = New Integer() {1}
setGateway = objMO.InvokeMethod("SetGateways", newGateway, Nothing)
Catch generatedExceptionName As Exception
Throw
End Try
End If
Next
End Sub
''' <summary>
''' Set's the DNS Server of the local machine
''' </summary>
''' <param name="NIC">NIC address</param>
''' <param name="DNS">DNS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setDNS(ByVal NIC As String, ByVal DNS As String)
Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If CBool(objMO("IPEnabled")) Then
' if you are using the System.Net.NetworkInformation.NetworkInterface you'll need to change this line to if (objMO["Caption"].ToString().Contains(NIC)) and pass in the Description property instead of the name
If objMO("Caption").Equals(NIC) Then
Try
Dim newDNS As ManagementBaseObject = objMO.GetMethodParameters("SetDNSServerSearchOrder")
newDNS("DNSServerSearchOrder") = DNS.Split(","c)
Dim setDNS As ManagementBaseObject = objMO.InvokeMethod("SetDNSServerSearchOrder", newDNS, Nothing)
Catch generatedExceptionName As Exception
Throw
End Try
End If
End If
Next
End Sub
''' <summary>
''' Set's WINS of the local machine
''' </summary>
''' <param name="NIC">NIC Address</param>
''' <param name="priWINS">Primary WINS server address</param>
''' <param name="secWINS">Secondary WINS server address</param>
''' <remarks>Requires a reference to the System.Management namespace</remarks>
Public Sub setWINS(ByVal NIC As String, ByVal priWINS As String, ByVal secWINS As String)
Dim objMC As New ManagementClass("Win32_NetworkAdapterConfiguration")
Dim objMOC As ManagementObjectCollection = objMC.GetInstances()
For Each objMO As ManagementObject In objMOC
If CBool(objMO("IPEnabled")) Then
If objMO("Caption").Equals(NIC) Then
Try
Dim setWINS As ManagementBaseObject
Dim wins As ManagementBaseObject = objMO.GetMethodParameters("SetWINSServer")
wins.SetPropertyValue("WINSPrimaryServer", priWINS)
wins.SetPropertyValue("WINSSecondaryServer", secWINS)
setWINS = objMO.InvokeMethod("SetWINSServer", wins, Nothing)
Catch generatedExceptionName As Exception
Throw
End Try
End If
End If
Next
End Sub
but gives error in ManagementClass() and other items. I imported System.Management. But vb shows error that it is not found.
但在 ManagementClass() 和其他项目中出错。我导入了 System.Management。但是 vb 显示找不到它的错误。
This is code i converted to print nic available in a pc. Is it correct? :
这是我转换为可在 PC 中使用的打印 nic 的代码。这是正确的吗?:
For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
If nic.OperationalStatus = OperationalStatus.Up Then
Debug.Print(nic.GetPhysicalAddress().ToString())
Exit For
End If
Next
but was is nic name to supply? or a example demo to use it?
但是要提供 nic 名称吗?或使用它的示例演示?
回答by Cody Gray
You've already found some people who were nice enough to write most of the code for you, now all you have to do is translate it to VB.NET. That shouldn't be hard for any real .NET programmer, given the extensive similarities between the two languages. But if you need a little extra help, try one of the free online translators, like this one from Developer Fusion.
您已经发现有些人非常友善,可以为您编写大部分代码,现在您要做的就是将其转换为 VB.NET。考虑到两种语言之间的广泛相似性,这对于任何真正的 .NET 程序员来说应该不难。但是,如果您需要一些额外的帮助,请尝试使用其中一种免费的在线翻译器,例如来自 Developer Fusion 的这个翻译器。
The only remaining issue, then, is how you know which NIC name to supply, since the linked code accepts a stringparameter specifying the NIC.
剩下的唯一问题是如何知道要提供哪个 NIC 名称,因为链接代码接受string指定 NIC的参数。
Unfortunately, that's not a question to which we (or anyone) can give you an exact answer. The issue is that a single computer can have multiple NICs installed, and each of them can have different DNS settings. That's why the function requires the parameter so that you can choose which one to configure.
不幸的是,这不是我们(或任何人)可以给您确切答案的问题。问题是一台计算机可以安装多个 NIC,并且每个 NIC 都可以有不同的 DNS 设置。这就是函数需要参数的原因,以便您可以选择要配置的参数。
The simple, straightforward approach is to enumerate all of the installed NICs, then
简单直接的方法是枚举所有已安装的 NIC,然后
- if there is only one, use that one.
- if there is more than one, display a dialog that lists the name of each and asks the user to choose.
- 如果只有一个,就用那个。
- 如果有多个,则显示一个对话框,列出每个的名称并要求用户选择。
Observe that the linked code uses the Captionproperty of the Win32_NetworkAdapterConfigurationclass as the "name" of the NIC. This is probably the same name that you should display to the user, along with any of the other property values that you think might contain useful information to help them in making a decision.
观察链接代码使用类的Caption属性Win32_NetworkAdapterConfiguration作为 NIC 的“名称”。这可能与您应该向用户显示的名称相同,以及您认为可能包含有助于他们做出决定的有用信息的任何其他属性值。
Asking the user is almost always a cop-out approach, considering that the user does not care and most likely does not know the answer. Good UI design keeps stuff like this to a bare minimum. But here, you really do not have much choice. Rationalize the choice with the logic that if a user has more than one NIC installed on her computer, she is probably an advanced user who will know how to answer your question.
考虑到用户并不关心并且很可能不知道答案,询问用户几乎总是一种逃避方法。良好的 UI 设计将此类内容保持在最低限度。但在这里,你真的没有太多选择。如果用户在她的计算机上安装了多个 NIC,那么她可能是一个知道如何回答您的问题的高级用户,从而使选择合理化。
Modifying the code to enumerate all of the installed NICs is relatively straightforward. Starting from Marc's refactored code:
修改代码以枚举所有已安装的 NIC 相对简单。从Marc 的重构代码开始:
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"] && objMO["Caption"].Equals(nic)))
we just need to remove the equality test applied to the Captionproperty:
我们只需要删除应用于Caption属性的相等测试:
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"]))
回答by Nick
You need to reference System.management as well as import it.
您需要引用 System.management 并导入它。

![制作一组单选按钮 [VB.NET]](/res/img/loading.gif)