[英]Namespace Microsoft.Win32 could not be found
I installed Visual Studio yesterday. 我昨天安装了Visual Studio。 I would like to see what version of the .NET Framework is installed with Visual Studio, so I followed this code from Microsoft . 我想看看Visual Studio附带了什么版本的.NET Framework,所以我遵循了Microsoft的这段代码 。 (scroll a bit down for the code). (向下滚动代码)。 I opened a new Visual C# project with 'Console App (.NET Core)' and copied the given code in it. 我使用“控制台应用程序(.NET Core)”打开了一个新的Visual C#项目,并在其中复制了给定的代码。
using System;
using Microsoft.Win32;
public class GetDotNetVersion
{
public static void Main()
{
GetDotNetVersion.Get45PlusFromRegistry();
}
private static void Get45PlusFromRegistry()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
Console.WriteLine(".NET Framework Version: " + CheckFor45PlusVersion((int)ndpKey.GetValue("Release")));
}
else
{
Console.WriteLine(".NET Framework Version 4.5 or later is not detected.");
}
}
}
// Checking the version using >= will enable forward compatibility.
private static string CheckFor45PlusVersion(int releaseKey)
{
if (releaseKey >= 461808)
return "4.7.2 or later";
if (releaseKey >= 461308)
return "4.7.1";
if (releaseKey >= 460798)
return "4.7";
if (releaseKey >= 394802)
return "4.6.2";
if (releaseKey >= 394254)
return "4.6.1";
if (releaseKey >= 393295)
return "4.6";
if (releaseKey >= 379893)
return "4.5.2";
if (releaseKey >= 378675)
return "4.5.1";
if (releaseKey >= 378389)
return "4.5";
// This code should never execute. A non-null release key should mean
// that 4.5 or later is installed.
return "No 4.5 or later version detected";
}
}
// This example displays output like the following:
// .NET Framework Version: 4.6.1
However, in the editor, the 'RegistryKey' is giving an error: "Namespace cannot be found." 但是,在编辑器中,“ RegistryKey”给出了错误:“找不到命名空间。” The second line 'using Microsoft.Win32;' 第二行“使用Microsoft.Win32;” is grey as if it is not called upon in the code. 是灰色的,好像未在代码中调用它一样。
When I look in the Solution Explorer panel in Visual Studio, I can list the dependencies. 在Visual Studio的“解决方案资源管理器”面板中查看时,可以列出依赖关系。 I do not see MicrosoftWin32.dll but I do see mscorlib.dll. 我没有看到MicrosoftWin32.dll,但是我看到了mscorlib.dll。 I read somewhere that the Microsoft.Win32 namespace could be found there? 我读过某个地方可以找到Microsoft.Win32命名空间?
Can someone give me some clues as how to resolve this? 有人可以给我一些解决方法的线索吗? Where can I find Microsoft.Win32 namespace? 在哪里可以找到Microsoft.Win32命名空间? Why is it not available in this standard project? 为什么在此标准项目中不可用?
If you use netcore you need to add package Microsoft.Win32.Registry: 如果使用netcore,则需要添加软件包Microsoft.Win32.Registry:
Your project: 您的项目:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Win32.Registry" Version="4.4.0" />
</ItemGroup>
</Project>
Result: 结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.