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 ther开发者_如何学Ce'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.)?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.
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
}
精彩评论