Created
June 2, 2018 06:22
-
-
Save nogawanogawa/7ab61ed4cea5661eb56b2ad9f9f164fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string GetMyIPAddress() | |
{ | |
var icp = NetworkInformation.GetInternetConnectionProfile(); | |
if(icp == null) | |
return string.Empty; | |
var adapter = icp.NetworkAdapter; | |
if(adapter == null) | |
return string.Empty; | |
var hostname = NetworkInformation.GetHostNames().FirstOrDefault( | |
hn => hn.IPInformation?.NetworkAdapter != null | |
&& hn.Type == HostnameType.Ipv4 | |
&& hn.IPInformation.NetworkAdapter.NetworkAdapterId == icp.NetworkAdapter.NetworkAdapterId | |
); | |
return hostname.CanonicalName; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment