【问题标题】:Getting the .NET Framework directory path获取 .NET Framework 目录路径
【发布时间】:2010-09-27 09:54:54
【问题描述】:

如何在我的 C# 应用程序中获取 .NET Framework 目录路径?

我指的文件夹是“C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727”

【问题讨论】:

    标签: c# .net frameworks directory


    【解决方案1】:

    对于 .NET Framework 版本 >= 4.5,an official way from MSDN

    internal static class DotNetFrameworkLocator
    {
        public static string GetInstallationLocation()
        {
            const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
            using (var ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
            {
                if (ndpKey == null)
                    throw new Exception();
    
                var value = ndpKey.GetValue("InstallPath") as string;
                if (value != null)
                    return value;
                else
                    throw new Exception();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      当前.NET应用程序活动的CLR的安装目录路径可以通过以下方法获得:

      System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
      

      我会强烈建议不要直接阅读注册表。例如,当 .NET 应用程序在 64 位系统中运行时,CLR 可以从“C:\Windows\Microsoft.NET\Framework64\v2.0.50727”(AnyCPU,x64 编译目标)或“C:\ Windows\Microsoft.NET\Framework\v2.0.50727"(x86 编译目标)。读取注册表不会告诉您当前 CLR 使用了两个目录中的哪一个。

      另一个重要的事实是,对于 .NET 2.0、.NET 3.0 和 .NET 3.5 应用程序,“当前 CLR”将是“2.0”。这意味着即使在 .NET 3.5 应用程序(从 3.5 目录加载它们的一些程序集)中,GetRuntimeDirectory() 调用也将返回 2.0 目录。根据您对术语“.NET Framework 目录路径”的解释,GetRuntimeDirectory 可能不是您要查找的信息(“CLR 目录”与“来自 3.5 程序集的目录”)。

      【讨论】:

      • 我相信这是正确的答案,应该被选中。感谢您澄清 GetRuntimeDirectory 即使在 3.0 或 3.5 应用程序上也始终返回 2.0 文件夹这一事实。在大多数情况下,这是正确的行为,您希望访问 2.0 中的框架工具(但不是 3.0 3.5)。
      • 如何在 64 位系统上获取适用于 x86 和 x64 .NET 框架的 InstallRoot? “[HKLM]\Software\Microsoft.NetFramework\InstallRoot”是否总是指向 x86 版本的 .NET,即使在 64 位系统上也是如此?我需要使用非托管应用程序获取此文件夹的路径,所以我不能使用您上面列出的方法。谢谢。
      • 可能最简单的方法是创建微型 .NET 应用程序,一个为 x86 编译(例如“getdotnetpath32.exe”),另一个为 x64 编译(例如“getdotnetpath64.exe”)。托管应用程序将使用 GetRuntimeDirectory() 调用并将其写入 STDOUT (Console.Output)。然后,非托管应用程序将为 x86 (getdotnetpath32.exe) 启动一个子进程,将其 STDOUT 连接到其内存流并读取该进程产生的内容。然后它将为 x64 (getdotnetpath64.exe) 启动一个子进程,将其 STDOUT 连接到其内存流并读取该进程产生的内容
      • 如果您想要存储配置文件的目录的路径,您可以执行以下操作: var readFromDirectory = System.IO.Path.GetDirectoryName(System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile) ; var mediumTrustConfigPath = System.IO.Path.Combine(readFromDirectory, "web_mediumtrust.config");
      【解决方案3】:

      更简单的方法是包含 Microsoft.Build.Utilities 程序集并使用

      using Microsoft.Build.Utilities;
      ToolLocationHelper.GetPathToDotNetFramework(
              TargetDotNetFrameworkVersion.VersionLatest);
      

      【讨论】:

      • 这听起来好多了,尤其是在使用影响构建过程的工具时。
      • 我猜这不适用于 .NET Core 和 .NET 5.0?
      【解决方案4】:

      读取 [HKLM]\Software\Microsoft.NetFramework\InstallRoot 键的值 - 您将获得“C:\WINDOWS\Microsoft.NET\Framework”。然后附加所需的框架版本。

      【讨论】:

        【解决方案5】:

        您可以从 Windows 注册表中获取它:

        using System;
        using Microsoft.Win32;
        

        // ...

        public static string GetFrameworkDirectory()
        {
          // This is the location of the .Net Framework Registry Key
          string framworkRegPath = @"Software\Microsoft\.NetFramework";
        
          // Get a non-writable key from the registry
          RegistryKey netFramework = Registry.LocalMachine.OpenSubKey(framworkRegPath, false);
        
          // Retrieve the install root path for the framework
          string installRoot = netFramework.GetValue("InstallRoot").ToString();
        
          // Retrieve the version of the framework executing this program
          string version = string.Format(@"v{0}.{1}.{2}\",
            Environment.Version.Major, 
            Environment.Version.Minor,
            Environment.Version.Build); 
        
          // Return the path of the framework
          return System.IO.Path.Combine(installRoot, version);     
        }
        

        Source

        【讨论】:

        • 我强烈建议不要直接访问注册表(在 64 位操作系统上,这实际上可能给出错误的答案)。有关详细信息,请参阅下面的答案。
        • @CMS 我想像 silvetlight 版本一样做。如果我要删除注册表项,这意味着软件也会从系统中卸载。谢谢
        猜你喜欢
        • 2019-11-27
        • 1970-01-01
        • 1970-01-01
        • 2023-03-02
        • 2017-03-05
        • 2014-11-08
        • 2014-04-09
        • 2010-10-14
        • 1970-01-01
        相关资源
        最近更新 更多