- 1 year ago
How get device's ip those connected to local network in Windows IoT Core/UWP
To get the IP addresses of devices connected to the local network using Windows IoT Core/UWP, you can use the Windows.Networking.Connectivity
namespace. Here's a simple example of how to achieve this:
csharpusing System;
using Windows.Networking;
using Windows.Networking.Connectivity;
using System.Linq;
using System.Collections.Generic;
namespace LocalNetworkScanner
{
class Program
{
static void Main(string[] args)
{
List<HostName> deviceHostNames = GetLocalDevices();
foreach (var hostName in deviceHostNames)
{
Console.WriteLine($"Device Name: {hostName.DisplayName}");
Console.WriteLine($"IP Address: {hostName.CanonicalName}");
Console.WriteLine("--------------------------");
}
}
static List<HostName> GetLocalDevices()
{
var hostNames = NetworkInformation.GetHostNames().ToList();
List<HostName> deviceHostNames = new List<HostName>();
foreach (var hostName in hostNames)
{
if (hostName.Type == HostNameType.Ipv4 || hostName.Type == HostNameType.Ipv6)
{
deviceHostNames.Add(hostName);
}
}
return deviceHostNames;
}
}
}
This code uses the NetworkInformation.GetHostNames()
method to get a list of host names for the current device. It then filters out the IP addresses (IPv4 and IPv6) and displays them along with the host names.
Make sure to add the necessary references to your UWP project, including Windows.Foundation.UniversalApiContract
and Windows.Networking.Connectivity
.
Please note that the code above will list all the IP addresses associated with the device, including loopback addresses. You may want to filter the results further based on your specific requirements.