如何在visual basi.net中更改这两个值。我不想用vbscript来做这件事。
我搜索并得到了这个 - How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#
^转换代码:
''' <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
但在ManagementClass()和其他项目中出错。我导入了System.Management。但是vb显示没有找到它的错误。
这是我在PC中转换为打印的代码。这是对的吗? :
For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
If nic.OperationalStatus = OperationalStatus.Up Then
Debug.Print(nic.GetPhysicalAddress().ToString())
Exit For
End If
Next
但是供应的名字是什么?或者使用它的示例演示?
答案 0 :(得分:1)
您已经找到some people who were nice enough to write most of the code for you,现在您要做的就是将其翻译为VB.NET。鉴于两种语言之间存在广泛的相似性,对于任何真正的.NET程序员来说,这应该不难。但是,如果您需要一些额外的帮助,请尝试免费的在线翻译,例如this one from Developer Fusion。
唯一剩下的问题是,您知道要提供哪个NIC名称,因为链接代码接受指定NIC的string
参数。
不幸的是,这不是我们(或任何人)可以给你一个确切答案的问题。问题是,一台计算机可以安装多个NIC,并且每个NIC都可以具有不同的DNS设置。这就是函数需要参数的原因,以便您可以选择要配置的参数。
简单直接的方法是枚举所有已安装的NIC,然后
观察链接代码使用Caption
类的Win32_NetworkAdapterConfiguration
属性作为NIC的“名称”。这可能与您应向用户显示的名称一起,以及您认为可能包含有助于他们做出决定的有用信息的任何其他属性值。
Asking the user is almost always a cop-out approach,考虑到用户不在乎并且很可能不知道答案。良好的UI设计可以将这样的东西保持在最低限度。但在这里,你真的没有太多选择。合理化选择的逻辑是,如果用户在她的计算机上安装了多个NIC,她可能是一个知道如何回答问题的高级用户。
修改代码以枚举所有已安装的NIC相对简单。从Marc's refactored code开始:
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"] && objMO["Caption"].Equals(nic)))
我们只需要删除应用于Caption
属性的等式测试:
foreach (var managementObject in networkConfigs.Cast<ManagementObject>().Where(objMO => (bool)objMO["IPEnabled"]))
答案 1 :(得分:0)
您需要引用System.management并导入它。