Skip to content

Instantly share code, notes, and snippets.

@nogawanogawa
Created June 2, 2018 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nogawanogawa/7ab61ed4cea5661eb56b2ad9f9f164fe to your computer and use it in GitHub Desktop.
Save nogawanogawa/7ab61ed4cea5661eb56b2ad9f9f164fe to your computer and use it in GitHub Desktop.
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