There are different ways to find out manually that whether we're connecte to some network or not. But how we do this via programming - especially in .Net? In this post I'll give you the idea to find out that if we're connected to the network or not. This code snippet can also give us the number of the networks available to our machine.
In order to find out connected networks in Microsoft.Net using either C# or Visual Basic.Net we can use System.Management namespace. There are two classes available in this namespace that can help us achieve this goal. These two calsses are ObjectQuery and ManagementObjectSearcher. The following code snippet first creates a query object that will be used to get all the networks available to our machine and then the ManagementObjectSearcher class' object will execute this query to return the results.
ObjectQuery objQuery = new ObjectQuery("select * from Win32_NetworkAdapter Where NetConnectionStatus=2");
Here NetConnectionStatus=2 means the network connections that are connected right now. Normally, these are the connections which are connected to the internet i.e. Cable, Wireless internet connection etc.
ManagementObjectSearcher searcher = new ManagementObjectSearcher(objQuery);
int connectednetworks = searcher.Get().Count;
if (connectednetworks <= 0)
return false;
No comments:
Post a Comment