Get properties of the current network connection

How do you define or view the connection profile of the current network connection (if any)?

In particular, I need to determine if the current connection is on a private or public network, and from there to determine whether network discovery is on or off.

This information seems to be easily accessible in the Windows Store app using Windows.Networking.Connectivity.NetworkInformation.GetConnectionProfiles () or NetworkInformation.GetInternetConnectionProfile () , but this is a standard desktop application that should run on Win 7 and Server 2008, as well as like Win 8 and Server 2012.

Enumerating the network adapters on the machine is not a problem, but this does not solve my problem - I need to get the properties and not the physical device.

Is there a built-in way to do this using the .Net framework? Or can this be done with WMI? Or as a crude alternative, can this be done by calling the netsh command (although this seems to depend on dot3svc and / or wlansvc to be executed)?

+7
c # networking network-programming
source share
1 answer

For this purpose, you can use the Network List Manager API to use it from the C # type list manager list type library (to directly compile this example below, uncheck the "Insert interaction types" checkbox in the link properties).

Then you should list all the connected networks, because there can be several, for example, right now I'm connected to the Internet and VPN. Then, for all connected networks, call GetCategory () API, it returns NLM_NETWORK_CATEGORY (private, public or domain).

Here is a sample code:

  var manager = new NetworkListManagerClass(); var connectedNetworks = manager.GetNetworks(NLM_ENUM_NETWORK.NLM_ENUM_NETWORK_CONNECTED).Cast<INetwork>(); foreach (var network in connectedNetworks) { Console.Write(network.GetName() + " "); var cat = network.GetCategory(); if (cat == NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_PRIVATE) Console.WriteLine("[PRIVATE]"); else if (cat == NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_PUBLIC) Console.WriteLine("[PUBLIC]"); else if (cat == NLM_NETWORK_CATEGORY.NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED) Console.WriteLine("[DOMAIN]"); } Console.ReadKey(); 

To discover the network, you must use the firewall API and refer to the NetFwTypeLib COM library and get INetFwProfile for the active profile, and then the services have file sharing, network discovery and remote Desktop Services, and there is a bool flag if they are enabled. Here is a sample code: (just to warn you that I did not use the code below in production, I just studied this API)

  Type objectType = Type.GetTypeFromCLSID(new Guid("{304CE942-6E39-40D8-943A-B913C40C9CD4}")); var man = Activator.CreateInstance(objectType) as INetFwMgr; /// get current profile INetFwProfile prof = man.LocalPolicy.CurrentProfile; Console.WriteLine("Current profile "); ShowProfileServices(prof); 

And a method that shows profile services.

 private static void ShowProfileServices(INetFwProfile prof) { var services = prof.Services.Cast<INetFwService>(); var sharing = services.FirstOrDefault(sc => sc.Name == "File and Printer Sharing"); if (sharing != null) Console.WriteLine(sharing.Name + " Enabled : " + sharing.Enabled.ToString()); else Console.WriteLine("No sharing service !"); var discovery = services.FirstOrDefault(sc => sc.Name == "Network Discovery"); if (discovery != null) Console.WriteLine(discovery.Name + " Enabled : " + discovery.Enabled.ToString()); else Console.WriteLine("No network discovery service !"); var remoteDesktop = services.FirstOrDefault(sc => sc.Name == "Remote Desktop"); if (remoteDesktop != null) Console.WriteLine(remoteDesktop.Name + " Enabled : " + remoteDesktop.Enabled.ToString()); else Console.WriteLine("No remote desktop service !"); } 
+10
source share

All Articles