Bagikan melalui


How to retrieve network adapter and locality information (HTML)

This topic demonstrates how to retrieve LanIdentifier objects associated with adapters on a network, and access information that can be used to determine its relative location within the network infrastructure using classes in the Windows.Networking.Connectivity namespace.

A LanIdentifier object defines methods an app uses to retrieve infrastructure/port ID values for determining location, and the ID value associated with a network adapter. This ID is expressed as a ConnectionProfile property to show its association with the network connection.

What you need to know

Technologies

Prerequisites

The following examples use JavaScript and are based on the Network information sample. For general help creating a Windows Runtime app using JavaScript, see Create your first Windows Runtime app using JavaScript.

Retrieve all LanIdentifier objects

Our first example function calls NetworkInformation.getLanIdentifiers method to retrieve an array of LanIdentifier objects.

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;

function DisplayLanIdentifiers() {
     var lanIdentifier = "";
     try {
         var lanIdentifiers = networkInfo.getLanIdentifiers();
         if (lanIdentifiers.length !== 0) {
             lanIdentifier += "Number of LAN Identifiers retrieved: " + lanIdentifiers.length + "\n\r";
             lanIdentifier += "=============================================\n\r";
             for (var i = 0; i < lanIdentifiers.length; i++) {
                 //Display Lan Identifier data for each identifier
                 lanIdentifier += getLanIdentifierData(lanIdentifiers[i]);
                 lanIdentifier += "----------------------------------------------------------------\n\r";
             }
             mySample.displayStatus(lanIdentifier);
         }
         else {
             mySample.displayStatus("No LAN identifier data found");
         }
     }

     catch (e) {
         mySample.displayError("Exception Caught: " + e + "\n\r");
     }
 }

Display the properties of a LanIdentifier object

We then pass a LanIdentifier object returned in the previous step to the following example function to display the specific infrastructure ID, port ID, and network adapter ID values.

var networkInfo = Windows.Networking.Connectivity.NetworkInformation;
  
function getLanIdentifierData(lanIdentifier) {
    var lanIdentifierData = "";
    var i = 0;
    try {
        if (lanIdentifier === null) {
            return "";
        }
        if (lanIdentifier.infrastructureId !== null) {
            lanIdentifierData += "Infrastructure Type: " + lanIdentifier.infrastructureId.type + "\n\r";
            lanIdentifierData += "Infrastructure Value: [";
            for (i = 0; i < lanIdentifier.infrastructureId.value.length; i++) {
                //Display the Infrastructure value array
                lanIdentifierData += lanIdentifier.infrastructureId.value[i].toString(16) + " ";
            }
            lanIdentifierData += "]\n\r";
        }
        if (lanIdentifier.portId !== null) {
            lanIdentifierData += "Port Type : " + lanIdentifier.portId.type + "\n\r";
            lanIdentifierData += "Port Value: [";
            for (i = 0; i < lanIdentifier.portId.value.length; i++) {
                //Display the PortId value array
                lanIdentifierData += lanIdentifier.portId.value[i].toString(16) + " ";
            }
            lanIdentifierData += "]\n\r";
        }
        if (lanIdentifier.networkAdapterId !== null) {
            lanIdentifierData += "Network Adapter Id : " + lanIdentifier.networkAdapterId + "\n\r";
        }
    }
    catch (e) {
        mySample.displayError("Exception Caught: " + e + "\n\r");
    }
    return lanIdentifierData;
}

Other

Create your first Windows Runtime app using JavaScript

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

LanIdentifier

LanIdentifierData

NetworkAdapter

NetworkInformation

NetworkInformation.getLanIdentifiers

Samples

Network information sample

Network status background sample