DNS 服务器更改事件?

发布于 2024-10-13 03:48:06 字数 350 浏览 2 评论 0原文

我正在编写一个在 Windows 上运行的 .Net 4 应用程序,用于监视 PC 当前是否连接到特定的内部网络。为了确定网络连接是否存在,我计划测试 DNS 名称解析 - 如果成功,则 PC 在网络上。 (如果有更好的方法,而且不太需要网络的话,请分享!)

我需要知道的是,我可以在代码中设置一个事件处理程序(或其他内容),当客户端 PC 的网络 DNS 查找服务器发生更改时触发该事件处理程序吗?例如,如果客户端通过 VPN 客户端连接,则新的 DNS 查找服务器将添加到网络环境中。作为响应,我的代码应该(重新)尝试 DNS 名称解析。

有人知道如何设置这样的事件处理程序吗?或者,是否有更好的网络变化事件需要关注(例如 VPN 连接上线等)?

I'm writing a .Net 4 app to run on Windows that monitors whether a PC is currently connected to a specific internal network. To determine whether the network connection exists, I plan to test a DNS name resolution - if successful, the PC is on the network. (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? For example, if the client connects via a VPN client, then new DNS lookup servers would be added to the network environment. In response, my code should (re)attempt the DNS name resolution.

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.)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南汐寒笙箫 2024-10-20 03:48:06

我不知道当 DNS 列表更改时“触发”任何特定的 .net 事件,您肯定可以监视网络状态并引发自定义事件(如果您检测到任何有趣的更改),然后在附加到该自定义的处理程序中执行某些代码事件。希望这有帮助。

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. Hope this helps.

行至春深 2024-10-20 03:48:06

作为另一种选择,您是否考虑过使用 WMI 尝试连接到远程计算机?只要您具有管理权限并且 WMI 对象安装在远程计算机上,您就可以测试连接。例如

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

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

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

As another option, have you considered using WMI to attempt to connect to the remote machine? 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. e.g.

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

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

try
{
   ms.Connect();
}
catch(Exception ex)
{
  // Connection Failure
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文