How to get the local host name in C # in a universal Windows 10 application

string machineName = System.Environment.MachineName; 

This code does not work for me, the error code "Environment" does not contain a definition for "MachineName"

I am using Visual Studio 2015 and a generic C # application

Please also specify the namespace that I need to use when you submit the response.

+6
c # windows uwp hostname
source share
1 answer

You need to use NetworkInformation.GetHostNames .

 var hostNames = Windows.Networking.Connectivity.NetworkInformation.GetHostNames(); 

But note that this method will return multiple HostName s.

In my case, it returns four HostName , of which DisplayName is MyComputerName, MyComputerName.local plus two IP addresses.

So it's probably safe to do this -

 using Windows.Networking; using Windows.Networking.Connectivity; var hostNames = NetworkInformation.GetHostNames(); var hostName = hostNames.FirstOrDefault(name => name.Type == HostNameType.DomainName)?.DisplayName ?? "???"; 
+13
source share

All Articles