简体   繁体   English

.NET Standard 1.4的Environment.MachineName等效项

[英]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. 我正在创建一个将在WPF项目和.NET Core项目中使用的类库。 I am trying to get the name of the machine using my application. 我正在尝试使用我的应用程序获取计算机的名称。

In both .NET Core and the WPF application I can use Environment.MachineName value. 在.NET Core和WPF应用程序中,我都可以使用Environment.MachineName值。 However in my .NET Standard Class library I cannot. 但是,在我的.NET标准类库中,我不能。

I get the following error: 我收到以下错误:

'Environment' does not contain a definition for 'MachineName' “环境”不包含“ MachineName”的定义

I tried doing what the answer suggested in this question but when I try to add System.Windows.Networking.Connectivity.NetworkInformation.GetHostNames() I get the following error: 我尝试做这个问题的答案,但是当我尝试添加System.Windows.Networking.Connectivity.NetworkInformation.GetHostNames() ,出现以下错误:

The name 'Windows' does not exist in the current context 名称“ Windows”在当前上下文中不存在

I assume that this only works for Windows 10 Universal Apps? 我认为这仅适用于Windows 10 Universal Apps? Either way I would prefer a platform independent way of getting the machine name (this way seems like its meant for windows machines only) 无论哪种方式,我都希望使用一种独立于平台的方式来获取计算机名称(这种方式似乎仅适用于Windows计算机)

Environment.MachineName is only available in .NET Standard > 1.5 using the System.Runtime.Extensions NuGet package (in .NET Standard 2.0 it is available automatically). Environment.MachineName仅在使用System.Runtime.Extensions NuGet包的.NET Standard> 1.5中可用(在.NET Standard 2.0中,它是自动可用的)。

As an alternative, you can either use System.Net.Dns.GetHostName() by referencing the System.Net.NameResolution NuGet package or resolve the COMPUTERNAME (win) or HOSTNAME (*nix) environment variables via Environment.GetEnvironmentVariable("COMPUTERNAME") . 或者,您可以通过引用System.Net.NameResolution NuGet包来使用System.Net.Dns.GetHostName() ,也可以通过Environment.GetEnvironmentVariable("COMPUTERNAME") )解析COMPUTERNAME (win)或HOSTNAME (* nix)环境变量。 Environment.GetEnvironmentVariable("COMPUTERNAME")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM