简体   繁体   English

DNS服务器更改事件?

[英]DNS servers changed event?

I'm writing a .Net 4 app to run on Windows that monitors whether a PC is currently connected to a specific internal network. 我正在编写.Net 4应用程序,以在Windows上运行,以监视PC当前是否已连接到特定的内部网络。 To determine whether the network connection exists, I plan to test a DNS name resolution - if successful, the PC is on the network. 为了确定是否存在网络连接,我计划测试DNS名称解析-如果成功,则PC在网络上。 (If there's a better way that isn't too network-heavy, please share!) (如果有一种更好的方法,它不太占用网络资源,请分享!)

What I need to know is, can I set up an event handler (or something) in my code that is triggered when the client PC's network DNS lookup servers change? 我需要知道的是,我可以在客户端PC的网络DNS查找服务器更改时触发的代码中设置事件处理程序吗? For example, if the client connects via a VPN client, then new DNS lookup servers would be added to the network environment. 例如,如果客户端通过VPN客户端连接,则新的DNS查找服务器将被添加到网络环境中。 In response, my code should (re)attempt the DNS name resolution. 作为响应,我的代码应(重新)尝试使用DNS名称解析。

Anyone know how to set up such an event handler? 有人知道如何设置这样的事件处理程序吗? Or, if there is a better network change event to watch for (like VPN connection coming online, etc.)? 或者,是否有更好的网络更改事件要监视(例如VPN连接上线等)?

I do not know any specific .net event "fired" when the DNS list changes, you could surely monitor the network status and raise a custom event if you detect any interesting change then you execute certain code in the handler you have attached to that custom event. 我不知道DNS列表更改时会触发任何特定的.net事件,如果您检测到任何有趣的更改,则可以肯定地监视网络状态并引发自定义事件,然后在附加到该自定义项的处理程序中执行某些代码事件。 Hope this helps. 希望这可以帮助。

As another option, have you considered using WMI to attempt to connect to the remote machine? 作为另一个选择,您是否考虑过使用WMI尝试连接到远程计算机? As long as you have administrative privilges and the WMI objects are installed on the remote machine, this would allow you to test your connection. 只要您具有管理权限,并且WMI对象已安装在远程计算机上,就可以测试连接。 eg 例如

var connection = new ConnectionOptions 
    { Username = "username", Password = "password" }

var ms = new ManagementScope(@"\\RemoteComputerName\root\CIMV2", connection);

try
{
   ms.Connect();
}
catch(Exception ex)
{
  // Connection Failure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM