2011-03-29 75 views
10

我的C#服务需要定期轮询nslookup host server。目前它产生了执行批处理脚本的Process。由于性能原因,我正在考虑使用一些API进行此项检查。但问题是,使用,例如System.Net.Dns.GetHostAddresses我只能模拟nslookup host检查,但不是nslookup host server(没有秒参数)。如何执行“nslookup主机服务器”

我看了一堆类似的SO问题,但他们都没有解决我的问题。

有没有什么办法在C#中执行nslookup host server而不使用一些沉重的第三方库?

回答

11

问题解决了!

http://msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx

见 “很好的例子,在这里” 部分

[DllImport("dnsapi", EntryPoint="DnsQuery_W", CharSet=CharSet.Unicode, SetLastError=true, ExactSpelling=true)] 
private static extern int DnsQuery([MarshalAs(UnmanagedType.VBByRefStr)]ref string pszName, QueryTypes wType, QueryOptions options, int aipServers, ref IntPtr ppQueryResults, int pReserved); 
[DllImport("dnsapi", CharSet=CharSet.Auto, SetLastError=true)] 
private static extern void DnsRecordListFree(IntPtr pRecordList, int FreeType); 

... 
0

我不知道如何使用框架直接执行此操作,因为我从来不需要使用与本地计算机配置中指定的服务器不同的DNS服务器。但你可能想试试this库。我从来没有使用它,但据我所知它可以让你指定一个DNS服务器来解析你传递的主机名。

1

我回头看了一眼。在标准类库中是不可能的,因此您将不得不使用外部组件来正确执行此操作。

有一些免费和付费的选择可供您选择。我的实施基于a posting on CodeProject,效果很好。 DNS Client Library for .NET(也被kprobst提及)在我完成我的作品后发布,或者我最初会使用这个作品。