Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the properties of the current network connection

How do you determine or examine the connection profile of the current network connection (if any)?

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

It seems like this information is readily available in a Windows Store app via the Windows.Networking.Connectivity.NetworkInformation.GetConnectionProfiles() or NetworkInformation.GetInternetConnectionProfile() functions, but this is a standard desktop app that must run on Win 7 and Server 2008 as well as Win 8 and Server 2012.

Enumerating the NICs on a machine is not a problem, but this doesn't solve my issue - I need to get the properties of the connection, not the physical device.

Is there an inbuilt way to do this with the .Net framework? Alternatively can it be done with WMI? Or as a crude alternative, can it be done by invoking the netsh command (although this seems to depend on the dot3svc and/or wlansvc services to be running)?

like image 870
slugster Avatar asked Oct 07 '13 23:10

slugster


1 Answers

You can use Network List Manager API for that purpose, to use it from C# import Network List Manager Type Library (to directly compile this example below uncheck Embed interop types in reference properties).

Then you must enumerate all connected networks, because there can be more than one, for example right now I am connected to internet and VPN. Then for all connected networks call GetCategory() API, it returns NLM_NETWORK_CATEGORY (private, public or domain).

Here is the 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();

For Network Discovery you have to use Firewall API and reference COM library NetFwTypeLib and get INetFwProfile for active profile, then in services there are File sharing, Network Discovery and Remote Desktop services, and there is a bool flag if these are Enabled. Here is the example code : (just to warn you I didn't use below code in production I was just exploring 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 the 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 !");
}
like image 85
Antonio Bakula Avatar answered Nov 09 '22 18:11

Antonio Bakula