Get name of network that a network interface is connected to
Solution 1
It turns out information about each network is stored as a 'network profile' by Windows, storing it's name and other info like whether it's public or not. The name can be changed by users in the control panel, but in my situation that's not a problem.
The Windows API Code Pack from Microsoft contains the APIs necessary to get the collection of network profiles. As it contains a lot of bloat that I don't need, the bare minimum code to wrap the Windows API can be found here.
A collection of the network profiles can then be found like so:
//Get the networks that are currently connected to
var networks = NetworkListManager.GetNetworks(NetworkConnectivityLevels.Connected);
Each object in the collection represents a network profile and contains a collection of NetworkConnection
objects. Each NetworkConnection
object appears to be info about an interface's connection to the base network.
foreach(Network network in networks)
{
//Name property corresponds to the name I originally asked about
Console.WriteLine("[" + network.Name + "]");
Console.WriteLine("\t[NetworkConnections]");
foreach(NetworkConnection conn in network.Connections)
{
//Print network interface's GUID
Console.WriteLine("\t\t" + conn.AdapterId.ToString());
}
}
The NetworkConnection.AdapterId
property is the same network interface GUID that the NetworkInterface.Id
property knows.
So, you can determine what network an interface is connected to, by checking if one of the network's connections have the same ID as the interface. Note that they're represented differently, so you'll have to do a bit more work:
Both my Wi-Fi and Ethernet interfaces are connected to the BELL024 network in the above example.
Solution 2
You can use WMI to query for your network name. You can use this code as a sample:
ManagementScope oMs = new ManagementScope();
ObjectQuery oQuery =
new ObjectQuery("Select * From Win32_NetworkAdapter");
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs, oQuery);
ManagementObjectCollection oReturnCollection = oSearcher.Get();
foreach (ManagementObject oReturn in oReturnCollection)
{
if (oReturn.Properties["NetConnectionID"].Value != null)
{
Console.WriteLine("Name : " + oReturn.Properties["NetConnectionID"].Value);
}
}
Solution 3
On Windows 8 and Windows 2012 and higher you can query WMI class MSFT_NetConnectionProfile from root/StandardCimv2 namespace.
It shouldn't be too hard to convert this to C#.
Get-WmiObject -Namespace root/StandardCimv2 -Class MSFT_NetConnectionProfile | Format-Table InterfaceAlias, Name
sawch
Updated on June 04, 2022Comments
-
sawch almost 2 years
In Windows Control Panel, you can find a list of network interfaces/connections which displays the following:
In the .NET framework these are represented in the
NetworkInterface
class (and found viaNetworkInterface.GetAllNetworkInterfaces
).For reference, say I'm reading properties from the Ethernet interface - the
NetworkInterface.Name
property returns"Ethernet"
, theNetworkInterface.Description
property returns"Realtek PCIe FE Family Controller"
.However, nothing in the class seems to be able to get me the name of the network it's connected to (in this case
"BELL024"
). How would I go about getting that string? I have to know what network the interface is associated with, not just a list of the networks that exist. -
sawch over 7 yearsThat only seems to return the name of the network interface (the same as
NetworkInterface.Name
property). I looped through all of the possible properties that method has and none of them provide the name of the network I'm looking for unfortunately. -
sawch about 7 yearsHint: Guid.Parse to easily convert
NetworkInterface.Id
string to the same type as NetworkConnection.AdapterId, then Guid.Equals(...)