更新时间:2023-11-19 21:07:34
您需要使用 NetworkInformation.GetHostNames
.
You need to use NetworkInformation.GetHostNames
.
var hostNames = Windows.Networking.Connectivity.NetworkInformation.GetHostNames();
但是注意这个方法会返回多个HostName
.
But note that this method will return multiple HostName
s.
就我而言,它返回四个 HostName
,其中 DisplayName
是 MyComputerName、MyComputerName.local加上两个 IP 地址.
In my case, it returns four HostName
s of which DisplayName
s are MyComputerName, MyComputerName.local plus two IP addresses.
所以我想这样做可能是安全的 -
So I guess it's probably safe to do -
using Windows.Networking;
using Windows.Networking.Connectivity;
var hostNames = NetworkInformation.GetHostNames();
var hostName = hostNames.FirstOrDefault(name => name.Type == HostNameType.DomainName)?.DisplayName ?? "???";