Query Local IP Address

后端 未结 2 1336
心在旅途
心在旅途 2020-12-07 20:46

I have the need to know my actual local IP address (i.e. not the loopback address) from a Windows 8 WinRT/Metro app. There are several reasons I need this.

2条回答
  •  情歌与酒
    2020-12-07 21:33

    About the accepted answer, you just need this:

    HostName localHostName = NetworkInformation.GetHostNames().FirstOrDefault(h =>
                        h.IPInformation != null &&
                        h.IPInformation.NetworkAdapter != null);
    

    You can get the local IP Address this way:

    string ipAddress = localHostName.RawName; //XXX.XXX.XXX.XXX
    

    Namespaces used:

    using System.Linq;
    using Windows.Networking;
    using Windows.Networking.Connectivity;
    

提交回复
热议问题