Visual Studio 2017 c#控制台项目不提供名称空间win32.registry

Visual Studio 2017 c#控制台项目不提供名称空间win32.registry,c#,vb.net,visual-studio-2017,C#,Vb.net,Visual Studio 2017,我注意到,当您创建VS2017 C#console项目并尝试访问 Microsoft.Win32.Registry 类,它似乎不是引用的一部分。因为它是mscorlib.dll的一部分,所以它应该是。添加mscorlib.dll作为参考,显然会导致基本类型的双重定义 更好的是:在VB中做同样的事情(控制台项目,使用注册表类),它马上就可以工作了 是我还是这是VS2017中的一个bug ---编辑--- 守则: using System; using Microsoft.Win32; publi

我注意到,当您创建VS2017 C#console项目并尝试访问

Microsoft.Win32.Registry

类,它似乎不是引用的一部分。因为它是
mscorlib.dll的一部分,所以它应该是。添加
mscorlib.dll
作为参考,显然会导致基本类型的双重定义

更好的是:在VB中做同样的事情(控制台项目,使用注册表类),它马上就可以工作了

是我还是这是VS2017中的一个bug

---编辑---

守则:

using System;
using Microsoft.Win32;

public class GetDotNetVersion
{
    public 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 >= 394802)
        return "4.6.2 or later";
    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";
}
}
// Calling the GetDotNetVersion.Get45PlusFromRegistry method produces 
// output like the following:
//       .NET Framework Version: 4.6.1

namespace ConsoleApp1
{
 class Program
 {
    static void Main(string[] args)
    {
        GetDotNetVersion.Get45PlusFromRegistry();
    }
 }
}
它是一个.netCoreApp 1.1

我得到错误CS0246(找不到类型或命名空间注册表键)和后续错误(总共4个)

VS还提示Microsoft.Win32的using指令已过时


谢谢

您的代码中确实有对Microsoft.Win32的引用,并尝试使用注册表类(例如Registry.GetValue())访问GetValue(或其他方法)


注册表是一个。

.NET核心是跨平台的,因此不建议使用仅限Windows的注册表访问等功能

如果确实需要,请使用NuGet软件包管理器添加Microsoft.Win32.RegistryKey软件包


什么类型的控制台应用程序。网络公司?这不是一个错误。这是一个BCL类,不是Visual Studio的功能。要么您创建了一个.NET核心项目,要么您的代码中有输入错误。发布一个可复制的示例代码如下: