[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

Retrieve DNS Settings

Is there a way to retrieve the dns settings from the local computer and then change those settings if they are not correct using vb.net?
0
TMcClendonx
Asked:
TMcClendonx
  • 2
1 Solution
 
grayeCommented:
I'd consider using Windows Management Instrumentation (WMI)

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_networkadapterconfiguration.asp

Here is a simple read-only example to demonstrate the concepts

mports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class MyWMIQuery

        Public Overloads Shared Function Main() As Integer

            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapterConfiguration")

                For Each queryObj As ManagementObject in searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_NetworkAdapterConfiguration instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Description: {0}", queryObj("Description"))
                    Console.WriteLine("DNSDomain: {0}", queryObj("DNSDomain"))

                    If queryObj("DNSDomainSuffixSearchOrder") Is Nothing Then
                        Console.WriteLine("DNSDomainSuffixSearchOrder: {0}", queryObj("DNSDomainSuffixSearchOrder"))
                    Else
                        Dim arrDNSDomainSuffixSearchOrder As String()
                        arrDNSDomainSuffixSearchOrder = queryObj("DNSDomainSuffixSearchOrder")
                        For Each arrValue As String In arrDNSDomainSuffixSearchOrder
                            Console.WriteLine("DNSDomainSuffixSearchOrder: {0}", arrValue)
                        Next
                    End If
                    Console.WriteLine("DNSEnabledForWINSResolution: {0}", queryObj("DNSEnabledForWINSResolution"))
                    Console.WriteLine("DNSHostName: {0}", queryObj("DNSHostName"))

                    If queryObj("DNSServerSearchOrder") Is Nothing Then
                        Console.WriteLine("DNSServerSearchOrder: {0}", queryObj("DNSServerSearchOrder"))
                    Else
                        Dim arrDNSServerSearchOrder As String()
                        arrDNSServerSearchOrder = queryObj("DNSServerSearchOrder")
                        For Each arrValue As String In arrDNSServerSearchOrder
                            Console.WriteLine("DNSServerSearchOrder: {0}", arrValue)
                        Next
                    End If
                    Console.WriteLine("DomainDNSRegistrationEnabled: {0}", queryObj("DomainDNSRegistrationEnabled"))
                    Console.WriteLine("FullDNSRegistrationEnabled: {0}", queryObj("FullDNSRegistrationEnabled"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function
    End Class
End Namespacemports System
Imports System.Management
Imports System.Windows.Forms

Namespace WMISample

    Public Class MyWMIQuery

        Public Overloads Shared Function Main() As Integer

            Try
                Dim searcher As New ManagementObjectSearcher( _
                    "root\CIMV2", _
                    "SELECT * FROM Win32_NetworkAdapterConfiguration")

                For Each queryObj As ManagementObject in searcher.Get()

                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Win32_NetworkAdapterConfiguration instance")
                    Console.WriteLine("-----------------------------------")
                    Console.WriteLine("Description: {0}", queryObj("Description"))
                    Console.WriteLine("DNSDomain: {0}", queryObj("DNSDomain"))

                    If queryObj("DNSDomainSuffixSearchOrder") Is Nothing Then
                        Console.WriteLine("DNSDomainSuffixSearchOrder: {0}", queryObj("DNSDomainSuffixSearchOrder"))
                    Else
                        Dim arrDNSDomainSuffixSearchOrder As String()
                        arrDNSDomainSuffixSearchOrder = queryObj("DNSDomainSuffixSearchOrder")
                        For Each arrValue As String In arrDNSDomainSuffixSearchOrder
                            Console.WriteLine("DNSDomainSuffixSearchOrder: {0}", arrValue)
                        Next
                    End If
                    Console.WriteLine("DNSEnabledForWINSResolution: {0}", queryObj("DNSEnabledForWINSResolution"))
                    Console.WriteLine("DNSHostName: {0}", queryObj("DNSHostName"))

                    If queryObj("DNSServerSearchOrder") Is Nothing Then
                        Console.WriteLine("DNSServerSearchOrder: {0}", queryObj("DNSServerSearchOrder"))
                    Else
                        Dim arrDNSServerSearchOrder As String()
                        arrDNSServerSearchOrder = queryObj("DNSServerSearchOrder")
                        For Each arrValue As String In arrDNSServerSearchOrder
                            Console.WriteLine("DNSServerSearchOrder: {0}", arrValue)
                        Next
                    End If
                    Console.WriteLine("DomainDNSRegistrationEnabled: {0}", queryObj("DomainDNSRegistrationEnabled"))
                    Console.WriteLine("FullDNSRegistrationEnabled: {0}", queryObj("FullDNSRegistrationEnabled"))
                Next
            Catch err As ManagementException
                MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
            End Try
        End Function
    End Class
End Namespace
0
 
grayeCommented:
...geez, somehow the example got pasted in there twice!
0
 
manchCommented:
U can use classes provided by vb.net

(Assembly System.Net have classes related to it)

Imports System.Net


Use the Net.DNS class object
(DNS Class is a part of the assembly System.Net)
(check whether the methods exposes by it r of used for u)





0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now