Login or Sign Up to become a member!
LessThanDot Site Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

VB.Net: Changing the clients DNS settings programatically

From Wiki

Jump to: navigation, search

If you don't have control over you DHCP server and when you have to set the clients DNS settings to another server manually this piece of code can help.

  1. Imports System.Management
  2.  
  3. Module Module1
  4.  
  5.     Sub Main()
  6.         GetNetworkInfo(My.Computer.Name)
  7.         Console.ReadLine()
  8.     End Sub
  9.  
  10.     Public Sub GetNetworkInfo(ByVal Machine As String)
  11.         Dim scope As New System.Management.ManagementScope("\\" + Machine + "\root\cimv2")
  12.         scope.Connect()
  13.         Dim objectQuery As New ObjectQuery("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionId IS NOT NULL")
  14.         Dim searcher As New ManagementObjectSearcher(scope, objectQuery)
  15.         Dim os As ManagementObject
  16.         Dim moColl As ManagementObjectCollection = searcher.Get()
  17.         Dim _list As String = ""
  18.         For Each os In moColl
  19.             Console.WriteLine(os("NetConnectionId"))
  20.             Console.WriteLine(ChangeAddress("set dns """ & os("NetConnectionId").ToString & """ static 200.0.0.60 primary"))
  21.             Console.WriteLine(ChangeAddress("add dns """ & os("NetConnectionId").ToString & """ 200.0.0.11 2"))
  22.         Next os
  23.     End Sub
  24.  
  25.     Private Function ChangeAddress(ByVal Argument As String) As String
  26.         Dim _Process As New Process()
  27.         _Process.StartInfo.RedirectStandardOutput = True
  28.         _Process.StartInfo.UseShellExecute = False
  29.         _Process.StartInfo.FileName = "C:\WINDOWS\system32\netsh"
  30.         _Process.StartInfo().Arguments = "interface ip " & Argument
  31.         _Process.Start()
  32.         Dim _StringBuilder As New System.Text.StringBuilder
  33.         _StringBuilder.Append("Executing: netsh " & _Process.StartInfo.Arguments & ControlChars.CrLf)
  34.         While Not _Process.StandardOutput.EndOfStream
  35.             _StringBuilder.Append(_Process.StandardOutput.ReadLine() & ControlChars.CrLf)
  36.         End While
  37.         Return _StringBuilder.ToString
  38.     End Function
  39.  
  40. End Module

586 Rating: 2.7/5 (19 votes cast)