Environment.MachineName equivalent for .NET Standard 1.4

I am creating a class library that will be used in a WPF project and a .NET Core project. I am trying to get the name of a machine using my application.

In .NET Core and a WPF application, I can use the value of Environment.MachineName . However, in my .NET Standard Class library I cannot.

I get the following error:

"Environment" does not contain a definition for "MachineName"

I tried to do what was suggested in this question , but when I try to add System.Windows.Networking.Connectivity.NetworkInformation.GetHostNames() , I get the following error:

The name "Windows" does not exist in the current context

I assume this only works for Windows 10 Universal Apps? In any case, I would prefer a platform-independent way to get the name of the machine (this method seems to be for window machines only)

+5
source share
1 answer

Environment.MachineName is only available in .NET Standard> 1.5, using the System.Runtime.Extensions NuGet package (in .NET Standard 2.0 it is automatically available).

Alternatively, you can use System.Net.Dns.GetHostName() by referencing the System.Net.NameResolution NuGet package or enable the environment variables COMPUTERNAME (win) or HOSTNAME (* nix) via Environment.GetEnvironmentVariable("COMPUTERNAME") .

+17
source

All Articles