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;
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 !"); }
Antonio Bakula
source share