based on @John Zhang's answer, but with a fix, don't throw a LINQ error about multiple matches and return an IPv4 address:
public static string GetLocalIp(HostNameType hostNameType = HostNameType.Ipv4) { var icp = NetworkInformation.GetInternetConnectionProfile(); if (icp?.NetworkAdapter == null) return null; var hostname = NetworkInformation.GetHostNames() .FirstOrDefault( hn => hn.Type == hostNameType && hn.IPInformation?.NetworkAdapter != null && hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId);
obviously, you can pass HostNameType.Ipv6 instead of Ipv4, which is the default (implicit) to get the IPv6 address
source share