简体   繁体   English

找不到命名空间Microsoft.Win32

[英]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.

相关问题 找不到Microsoft.Win32 - Microsoft.Win32 could not be found 是否可以直接使用Microsoft.Win32命名空间中的Win32Native类? - Is it possible to directly use the Win32Native class from the Microsoft.Win32 namespace? 为什么OpenFileDialog需要Microsoft.Win32? - Why OpenFileDialog requires Microsoft.Win32? 不使用 Microsoft.Win32 的 Openfiledailog 框 - Openfiledailog box without using Microsoft.Win32 .Net Standard Microsoft.Win32 不包含 OpenFileDialog() 方法 - .Net Standard Microsoft.Win32 does not contain OpenFileDialog() Method 即使使用Microsoft.Win32也找不到RegistryKey-Class - Can't find RegistryKey-Class even when Microsoft.Win32 is used Microsoft.Win32中的OpenFileDialog.FileName返回空字符串 - OpenFileDialog.FileName from Microsoft.Win32 returning empty strings 找不到类型&#39;Microsoft.Win32.SafeHandles.SafeRegistryHandle&#39;的构造方法 - Constructor on type 'Microsoft.Win32.SafeHandles.SafeRegistryHandle' not found Win Store应用程式中&#39;找不到类型或名称空间名称&#39;ICloneable&#39; - 'The type or namespace name 'ICloneable' could not be found' in win store app Microsoft翻译-找不到类型或名称空间名称&#39;DataContract&#39; - Microsoft Translate - The type or namespace name 'DataContract' could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM