简体   繁体   中英

Namespace Microsoft.Win32 could not be found

I installed Visual Studio yesterday. I would like to see what version of the .NET Framework is installed with Visual Studio, so I followed this code from 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.

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." The second line 'using 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. I do not see MicrosoftWin32.dll but I do see mscorlib.dll. I read somewhere that the Microsoft.Win32 namespace could be found there?

Can someone give me some clues as how to resolve this? Where can I find Microsoft.Win32 namespace? Why is it not available in this standard project?

If you use netcore you need to add package 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:

结果

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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