Condividi tramite


How to retrieve network adapter and locality information (XAML)

This topic demonstrates how to retrieve a LanIdentifier and access information that can be used to determine the location of a network adapter within the network infrastructure. The Network Information sample demonstrates this functionality.

A LanIdentifier object defines methods an app uses to retrieve infrastructure/port ID values for determining location, and the ID value associated with the network adapter itself, which is accessed via a ConnectionProfile to show association with a network connection.

What you need to know

Technologies

Prerequisites

The following examples use C# or C++ and are based on the Network information sample. For general help creating a Windows Runtime app using C# or Visual Basic, see Create your first Windows Runtime app using C# or Visual Basic. For general help creating a Windows Runtime app using C++, see Create your first Windows Runtime app using C++.

Retrieve all LanIdentifiers

Call NetworkInformation.GetLanIdentifiers method to retrieve each LanIdentifier. Each object is then passed to GetLanIdentifierData, defined in the next section, to access and display the individual property values.

using Windows.Networking.Connectivity;

void DisplayLanIdentifiers(object sender)
{
    string lanIdentifierData = string.Empty;
    var lanIdentifiers = NetworkInformation.GetLanIdentifiers();
    if (lanIdentifiers.Count != 0)
    {
        lanIdentifierData = "Number of Lan Identifiers retrieved: " + lanIdentifiers.Count + "\n";
        lanIdentifierData += "=============================================\n";
        for (var i = 0; i < lanIdentifiers.Count; i++)
        {
            //Display Lan Identifier data for each identifier
            lanIdentifierData += GetLanIdentifierData(lanIdentifiers[i]);
            lanIdentifierData += "------------------------------------------------\n";
        }
        LanIdentifiers.Text = lanIdentifierData;
    }
    else
    {
        LanIdentifiers.Text = "No Lan Identifier Data found";
    }
}

Display the properties of a LanIdentifier object

In the example provided above, each LanIdentifier is passed to GetLanIdentifier to display object data. Our next example defines GetLanIdentifier, which first verifies if an object property has a set value, and if it does, displays the value as a string.

using Windows.Networking.Connectivity;

string GetLanIdentifierData(LanIdentifier lanIdentifier)
{
    string lanIdentifierData = string.Empty;
    if (lanIdentifier == null)
    {
        return lanIdentifierData;
    }

    if (lanIdentifier.InfrastructureId != null)
    {
        lanIdentifierData += "Infrastructure Type: " + lanIdentifier.InfrastructureId.Type + "\n";
        lanIdentifierData += "Infrastructure Value: ";
        var infrastructureIdValue = lanIdentifier.InfrastructureId.Value;
        foreach (var value in infrastructureIdValue)
        {
            lanIdentifierData += value + " ";
        }
    }

    if (lanIdentifier.PortId != null)
    {
        lanIdentifierData += "\nPort Type : " + lanIdentifier.PortId.Type + "\n";
        lanIdentifierData += "Port Value: ";
        var portIdValue = lanIdentifier.PortId.Value;
        foreach (var value in portIdValue)
        {
            lanIdentifierData += value + " ";
        }
    }

    if (lanIdentifier.NetworkAdapterId != null)
    {
        lanIdentifierData += "\nNetwork Adapter Id : " + lanIdentifier.NetworkAdapterId + "\n";
    }
    return lanIdentifierData;
}

Other

Create your first Windows Runtime app using C# or Visual Basic

Create your first Windows Runtime app using C++

How to handle exceptions in network apps

How to manage metered network cost constraints

How to manage network connection events and changes in availability

How to retrieve network connection information

How to retrieve network connection usage data

Reference

NetworkAdapter

LanIdentifier

LanIdentifierData

NetworkInformation.GetLanIdentifiers

Windows.Networking.Connectivity

Samples

Network information sample

Network status background sample