Visual C# Developer Center > Visual C# Forums > Visual C# General > Check if DNS IP is obtained automatically or set static
Ask a questionAsk a question
 

Proposed AnswerCheck if DNS IP is obtained automatically or set static

  • Friday, November 06, 2009 9:17 AMp_f Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    How can I find out if the IP of a DNS Server (of a Network Interface) is obtained automatically? - I'd prefer NOT to use netsh calls or any other way which involves parsing files and console output. - Also I'd like not to read the Registry (unless it's the only way). Are there any Win API function? Operating Systems it should work on: Win XP, Win 7 Thanks in advance.

All Replies

  • Friday, November 06, 2009 9:50 AMsyntaxeater Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    I'll do ya one better.  Not only will I avoid netsh calls and registry info - I'll save you the Win API call and keep you in the .Net Fx:  (O.o)b

                IEnumerable<string> isDNSDynamic = from holdNI in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                                                   select string.Format("Interface {0} - IsDynamic? {1}",
                                                   holdNI.Name,
                                                   holdNI.GetIPProperties().IsDynamicDnsEnabled ? "Yes" : "No");
    
                foreach (string hold in isDNSDynamic)
                    Console.WriteLine(hold);
    
    • Proposed As Answer bysyntaxeater Monday, November 09, 2009 8:30 PM
    •  
  • Friday, November 06, 2009 11:17 AMp_f Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well I'm not entirely sure what IsDynamicDnsEnabled does, however it always returns true. According to MSDN it also is about the PC sending information to the DNS Server, not about how the DNS IP is retrieved :(. IsDynamicDnsEnabled: "Gets a Boolean value that indicates whether this interface is configured to automatically register its IP address information with the Domain Name System (DNS)."
  • Friday, November 06, 2009 4:01 PMsyntaxeater Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Right, to register it...  There by making it static?

    On an unused (or a new) network interface, change the connection to something static.  It can all be bogus values.

    Also, for this test, be sure to disable NetBIOS (found under advanced TCP/IP settings on the WINS tab).  Having it at "default" leaves the possibility for a dynamic value since it more or less says "if someone else already registered this with you, give me something else to use."  Which in and of itself is not DynamicEnabled, but the dynamic scenario exists if two nodes are ambiguous...  Which is why you'll still get "true" even though you provided static info.

    After that, test it and see.
  • Wednesday, November 11, 2009 8:07 AMp_f Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    IsDynamicDnsEnabled only tells you wheter the PC sends information to the DNS server, or not. It does not tell you if the DNS IP is configured automatically. So far I've found no API call / ... which tells if the DNS IP is configured static or provided by a DHCP.
  • Wednesday, November 11, 2009 7:37 PMsyntaxeater Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
                IEnumerable<string> isDNSDynamic = from holdNI in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
                                                   select string.Format("Interface {0} - IsDynamic? {1}",
                                                   holdNI.Name,
                                                   holdNI.GetIPProperties().GetIPv4Properties().IsDhcpEnabled ? "Yes" : "No");
    

    DHCP is different from dynamic.  As the example in the previous reply demonstrates; you can provide values and still end up dynamic.  If you check the above IsDHCPEnabled, that will indicate whether values are set or not on the local pc.  But in the event of false - that doesn't mean it's using a static configuration.