how to change this two values in visual basi.net. I do not want to use vbscript to do this.
i searched and get this - How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#
^ 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.
This is code i converted to print nic available in a pc. Is it correct? :
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?