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



我注意到,当您创建VS 2017 C#控制台项目并尝试访问

Microsoft.Win32.Registry

类,它似乎不是参考文献的一部分。因为它是mscorlib.dll的一部分,所以它应该。添加mscorlib.dll作为参考结果 - 显然 - 在基本类型的双重定义中。

更好的是:在 VB(控制台项目,使用 Registry 类(中做同样的事情,它会立即工作......

是我还是VS2017中的错误?

---编辑---

代码:

using System;
using Microsoft.Win32;
public class GetDotNetVersion
{
    public static void Get45PlusFromRegistry()
{
    const string subkey = @"SOFTWAREMicrosoftNET Framework SetupNDPv4Full";
    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(或其他方法((例如 Registry.GetValue(((

注册表是一个静态类。

.

NET Core是跨平台的,因此不建议仅Windows进行注册表访问等操作。

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

https://www.nuget.org/packages/Microsoft.Win32.Registry/

最新更新