visualbasi.netでこの2つの値を変更する方法。これを行うためにvbscriptを使用したくありません。
私はこれを検索して取得しました-C#のコードでネットワーク設定(IPアドレス、DNS、WINS、ホスト名)を変更するにはどうすればよいですか?
^変換されたコード:
''' <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で利用可能な印刷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
しかし、ニックネームは供給しましたか?またはそれを使用するためのサンプルデモ?