有没有办法从C#WPF应用程序中刷新DNS缓存?(在XP、Vista、Win7上)
有没有办法从C#WPF应用程序中刷新DNS缓存?该应用程序将在XP、Vista或Windows 7上运行。这可能是穷人的解决方案,但您可以使用有没有办法从C#WPF应用程序中刷新DNS缓存?(在XP、Vista、Win7上),c#,.net,caching,dns,flush,C#,.net,Caching,Dns,Flush,有没有办法从C#WPF应用程序中刷新DNS缓存?该应用程序将在XP、Vista或Windows 7上运行。这可能是穷人的解决方案,但您可以使用System.Diagnostics.Process启动ipconfig/flushdns您可以使用Microsoft“dnsapi.dll”中的函数。这将允许您完全以编程方式执行此操作: using System.Runtime.InteropServices; [DllImport("dnsapi.dll",EntryPoint="DnsFlushR
System.Diagnostics.Process
启动ipconfig/flushdns
您可以使用Microsoft“dnsapi.dll”中的函数。这将允许您完全以编程方式执行此操作:
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();
}
我已经测试过了,效果很好。试试这个-
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C ipconfig /flushdns";
process.StartInfo = startInfo;
process.Start();
我在互联网上搜索,却找不到一个dns条目。正在进行故障转移到备份数据中心的工作,这是我所需要的。我将dnsflushresolverchaentry_W进行了分解,得到了下面的p/invoke sig。我将DnsQuery方法与QueryNoWireFlag和NoHostFile标志一起使用。如果有任何结果,我会打电话给你 DNSFlushResolvercachentry_W。这可能是未来希望做同样事情的人的好信息
/// <summary>
/// Removes all related DNS records for the given hostname from the DNS resolver cache.
/// </summary>
/// <param name="hostName">The host name to flush from the resolver cache.</param>
/// <returns>Returns 1 if successful, zero if failed. No other error information is returned.</returns>
/// <remarks>
/// DnsFlushResolverCacheEntry_W is an undocumented method. From dissembler this method has two possible
/// return values 0 or 1. If the argument is null or the _R_ResolverFlushCacheEntry returns something
/// other than zero than the return value is zero. When _R_ResolverFlushCacheEntry returns zero the
/// return value is 1. Based off this and testing of the method it is assumed that 1 is used to indicate success.
/// After calling this method querying the DNS resolver cache returns no results.
///
/// __stdcall DnsFlushResolverCacheEntry_W(x)
/// ....
/// 6DC12729 xor esi, esi // Zero esi
/// 6DC1272B cmp [ebp+arg_0], esi // check if the hostname is null
/// 6DC1272E jz loc_6DC1D8B8 // Jump to end, return value is 0.
/// 6DC12734 mov [ebp+ms_exc.registration.TryLevel], esi
/// 6DC12737 push esi // Push 3 args for method, 2nd is hostname, others null
/// 6DC12738 push [ebp+arg_0]
/// 6DC1273B push esi
/// 6DC1273C call _R_ResolverFlushCacheEntry@12 // call this method
/// 6DC12741 mov [ebp+var_1C], eax // store return value in local
/// 6DC12744 mov [ebp+ms_exc.registration.TryLevel], 0FFFFFFFEh
/// 6DC1274B
/// 6DC1274B loc_6DC1274B:
/// 6DC1274B cmp [ebp+var_1C], esi
/// 6DC1274E jnz loc_6DC1D8E7 // Error? jump to block that does etw then return 0
/// 6DC12754 xor eax, eax // Success? Set eax to zero
/// 6DC12756 inc eax // result is 0, increment by 1. success return value is 1
/// 6DC12757 call __SEH_epilog4
/// 6DC1275C retn 4
///
/// 6DC1D8B8 xor eax, eax // set return value to zero
/// 6DC1D8BA jmp loc_6DC12757 // jump to end
///
/// 6DC1D8E7 mov eax, _WPP_GLOBAL_Control
/// 6DC1D8EC cmp eax, offset _WPP_GLOBAL_Control
/// 6DC1D8F1 jz short loc_6DC1D8B8
/// 6DC1D8F3 test byte ptr [eax+1Ch], 40h
/// 6DC1D8F7 jz short loc_6DC1D8B8 // This is probably testing some flag that is used to indicate if ETW is enabled
/// 6DC1D8F9 push [ebp+var_1C]
/// 6DC1D8FC push offset dword_6DC22494
/// 6DC1D901 push 0Dh
/// 6DC1D903 push dword ptr [eax+14h]
/// 6DC1D906 push dword ptr [eax+10h]
/// 6DC1D909 call _WPP_SF_q@20 ; WPP_SF_q(x,x,x,x,x) // This method does some ETW tracing
/// 6DC1D90E jmp short loc_6DC1D8B8
/// </remarks>
[DllImport("dnsapi.dll", EntryPoint = "DnsFlushResolverCacheEntry_W", CharSet = CharSet.Unicode)]
public static extern int DnsFlushResolverCacheEntry(string hostName);
//
///从DNS解析程序缓存中删除给定主机名的所有相关DNS记录。
///
///要从冲突解决程序缓存中刷新的主机名。
///如果成功,则返回1;如果失败,则返回零。不会返回其他错误信息。
///
///DNSFlushResolverCachentry是一种未记录的方法。从disembler来看,这种方法有两种可能
///返回值0或1。如果参数为null或_R_ResolverFlushCacheEntry返回某些内容
///除零以外,返回值为零。当_R_ResolverFlushCacheEntry返回零时
///返回值为1。基于此和方法测试,假设1表示成功。
///调用此方法后,查询DNS解析程序缓存不会返回任何结果。
///
///标准呼叫DNSFlushResolver客户中心W(x)
/// ....
///6DC12729异或esi,esi//零esi
///6DC1272B cmp[ebp+arg_0],esi//检查主机名是否为空
///6DC1272E jz loc_6DC1D8B8//跳转到末尾,返回值为0。
///6DC12734 mov[ebp+ms_exc.registration.TryLevel],esi
///6DC12737 push esi//push方法的3个参数,第二个是主机名,其他为空
///6DC12738推压[ebp+arg_0]
///6DC1273B推式esi
///6DC1273C呼叫_ResolverFlushCacheEntry@12//调用此方法
///6DC12741 mov[ebp+var_1C],eax//在本地存储返回值
///6DC12744 mov[ebp+ms_exc.registration.TryLevel],0FFFFFEH
///6DC1274B
///6DC1274B loc_6DC1274B:
///6DC1274B cmp[ebp+var_1C],esi
///6DC1274E jnz loc_6DC1D8E7//错误?跳转到执行etw的块,然后返回0
///6DC12754 xor eax,eax//成功?将eax设置为零
///6DC12756 inc eax//结果为0,增量为1。成功返回值为1
///6DC12757呼叫结束语4
///6DC1275C翻新4
///
///6DC1D8B8异或eax,eax//将返回值设置为零
///6DC1D8BA jmp loc_6DC12757//跳转到末端
///
///6DC1D8E7 mov eax,WPP全球控制
///6DC1D8EC cmp eax,偏移量WPP全局控制
///6DC1D8F1 jz短位置_6DC1D8B8
///6DC1D8F3测试字节ptr[eax+1Ch],40h
///6DC1D8F7 jz short loc_6DC1D8B8//这可能是在测试一些用于指示ETW是否启用的标志
///6DC1D8F9推送[ebp+var_1C]
///6DC1D8FC推送偏移量dword_6DC22494
///6DC1D901推送0Dh
///6DC1D903推送dword ptr[eax+14h]
///6DC1D906推送dword ptr[eax+10h]
///6DC1D909呼叫WPP SF_q@20 ; WPP_SF_q(x,x,x,x,x)//此方法执行一些ETW跟踪
///6DC1D90E jmp短loc_6DC1D8B8
///
[DllImport(“dnsapi.dll”,EntryPoint=“dnsflushresolvercachentry\u W”,CharSet=CharSet.Unicode)]
publicstaticextern int dnsflushresolvercachentry(字符串主机名);
试试这个:
static void flushDNS()
{
string flushDnsCmd = @"/C ipconfig /all";
try
{
var process = new Process
{
StartInfo = new ProcessStartInfo("cmd.exe", flushDnsCmd)
};
process.Start();
process.WaitForExit();
Console.WriteLine(String.Format("Successfully Flushed DNS:'{0}'", flushDnsCmd), EventLogEntryType.Information);
}
catch (Exception exp)
{
Console.WriteLine(String.Format("Failed to Flush DNS:'{0}' . Error:{1}", flushDnsCmd, exp.Message), EventLogEntryType.Error);
}
}
也许,挖掘pinvoke似乎是一种浪费。这是一个不错的退路-可能要等着看其他人是否知道.net framework中隐藏的东西-你可能会认为它在DNS类中,如果在任何地方,但我没有看到任何东西,只要不考虑到其他平台的可移植性,这似乎不是理想的解决方案,但dnsapi.dll PInvoke函数似乎不再存在了。@Aaron:可惜你没有给出答案。我会投赞成票的。@kbrimington,我觉得我在谷歌上找到的第一个结果(微笑)的答案是不对的:@亚伦:很公平。很好。3年后,这是第一个结果。那是第七站。通过运行终端进程来实现这一点似乎有点不妥。我希望原始海报会选择另一种方式。为什么不直接启动
ipconfig/flushdns
?我运行代码&它正在工作,但我如何知道该过程是否已成功清除DNS缓存?DnsFlushResolverCache()
是一个未记录的函数,这意味着它的未来是没有保障的。这可能曾经是有效的,但在Win10中尝试这一点目前产生了不正确的函数错误。它也不再列在msdn文档中,也不是我在NuGet中看到的PInvoke库的一部分。