Is there a way to flush the DNS cache from a C# WPF app? (on XP, Vista, Win7)

Greg picture Greg · Aug 17, 2010 · Viewed 12.8k times · Source

Is there a way to flush the DNS cache from a C# WPF app? The application would be running on either XP, Vista, or Windows 7.

Answer

Mike Webb picture Mike Webb · Feb 28, 2011

You can use a function from Microsoft's "dnsapi.dll". This will allow you to do this completely programmatically:

using System.Runtime.InteropServices;

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

public static void FlushMyCache() //This can be named whatever name you want and is the function you will call
{
    UInt32 result = DnsFlushResolverCache();
}

I have tested this and it works great.