string machineName = System.Environment.MachineName;
This code doesn't work for me, error code 'Environment' does not contain a definition for 'MachineName'
I'm using Visual Studio 2015 and Universal App with C#
Please also list the namespace I need to use when you post an 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
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 ?? "???";