title: change dns address using vb.net
body: <p><img src="https://i.stack.imgur.com/lyfnv.png" alt="enter image description here"></p>
  
  <p>how to change this two values in visual basi.net. I do not want to use vbscript to do this.</p>
  
  <p>i searched and get this - <a href="https://stackoverflow.com/questions/209779/how-can-you-change-network-settings-ip-address-dns-wins-host-name-with-code">How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#</a></p>
  
  <p>^ Converted code:</p>
  
  <pre><code>''' &lt;summary&gt;
  ''' Set's a new IP Address and it's Submask of the local machine
  ''' &lt;/summary&gt;
  ''' &lt;param name="ip_address"&gt;The IP Address&lt;/param&gt;
  ''' &lt;param name="subnet_mask"&gt;The Submask IP Address&lt;/param&gt;
  ''' &lt;remarks&gt;Requires a reference to the System.Management namespace&lt;/remarks&gt;
  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
  ''' &lt;summary&gt;
  ''' Set's a new Gateway address of the local machine
  ''' &lt;/summary&gt;
  ''' &lt;param name="gateway"&gt;The Gateway IP Address&lt;/param&gt;
  ''' &lt;remarks&gt;Requires a reference to the System.Management namespace&lt;/remarks&gt;
  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
  ''' &lt;summary&gt;
  ''' Set's the DNS Server of the local machine
  ''' &lt;/summary&gt;
  ''' &lt;param name="NIC"&gt;NIC address&lt;/param&gt;
  ''' &lt;param name="DNS"&gt;DNS server address&lt;/param&gt;
  ''' &lt;remarks&gt;Requires a reference to the System.Management namespace&lt;/remarks&gt;
  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
  ''' &lt;summary&gt;
  ''' Set's WINS of the local machine
  ''' &lt;/summary&gt;
  ''' &lt;param name="NIC"&gt;NIC Address&lt;/param&gt;
  ''' &lt;param name="priWINS"&gt;Primary WINS server address&lt;/param&gt;
  ''' &lt;param name="secWINS"&gt;Secondary WINS server address&lt;/param&gt;
  ''' &lt;remarks&gt;Requires a reference to the System.Management namespace&lt;/remarks&gt;
  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
  </code></pre>
  
  <p>but gives error in ManagementClass() and other items. I imported System.Management. But vb shows error that it is not found.</p>
  
  <p>This is code i converted to print nic available in a pc. Is it correct? :</p>
  
  <pre><code>    For Each nic As NetworkInterface In NetworkInterface.GetAllNetworkInterfaces()
          If nic.OperationalStatus = OperationalStatus.Up Then
              Debug.Print(nic.GetPhysicalAddress().ToString())
              Exit For
          End If
      Next
  </code></pre>
  
  <p>but was is nic name to supply? or a example demo to use it?</p>