Thursday, April 27, 2006

WINXP: Flushing DNS programatically using C#

In order to clear DNS resolver cache like what IPCONFIG /flushdns does, programmatically:

Requires dnsapi.dll which requires Win2000 or later. As usual, MS does not document the API, so your mileage may vary, but I think this is right:

using System.Runtime.InteropServices;

...

[DllImport("dnsapi.dll",EntryPoint="DnsFlushResolverCache")]
private static extern UInt32 DnsFlushResolverCache ();

...

public static void FlushResolverCache()
{
UInt32 result = DnsFlushResolverCache();
}

7 comments:

Anonymous said...

Is there anything to release flush renew the dns.

Thanks

Jorge Monasterio said...

Not sure what you are asking. Could you rephrase the question, please?

Anonymous said...

I need to perform all the below commands programmatically:

ipconfig /release

ipconfig /flushdns

ipconfig /renew

Anonymous said...

I tried using the DnsFlushResolverCache ();
but it is not updating the latest Ipaddress information. Is it a problem if we use it in a corporate LAN?

Anonymous said...

Actually I have a client server windows application with a one way communication. when the Ip address of the server changes. The client needs to update with the new ipaddress by using the hostname and reconnect to the server.

But when i use Dns.GetHostEntry(ipHostName). It is taking a lot of time for client to update with the new Ip address. like 45-50 attempts to reconnect.

Jorge Monasterio said...

Sorry, I don't have an answer, other than spawning a task to run: ipconfig /renew

You might want to try this FREE question and answer site:

http://StackOverflow.com

Unknown said...

Thank you very much for explaining this undocumented API :)