C# 获取 .NET Framework 目录路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/375860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 01:08:13  来源:igfitidea点击:

Getting the .NET Framework directory path

c#.netframeworksdirectory

提问by Bruno Cartaxo

How can I obtain the .NET Framework directory path inside my C# application?

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

The folder that I refer is "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727"

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

采纳答案by Milan Gardian

The path to the installation directory of the CLR active for the current .NET application can be obtained by using the following method:

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

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

I would stronglyadvice against reading the registry directly. For example, when a .NET application is running in 64bit systems, the CLR can either be loaded from "C:\Windows\Microsoft.NET\Framework64\v2.0.50727" (AnyCPU, x64 compilation targets) or from "C:\Windows\Microsoft.NET\Framework\v2.0.50727" (x86 compilation target). Reading registry will nottell you which one of the two directories was used by the current CLR.

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

Another important fact is that "the current CLR" will be "2.0" for .NET 2.0, .NET 3.0 and .NET 3.5 applications. This means that the GetRuntimeDirectory() call will return 2.0 directory even within .NET 3.5 applications (that load some of their assemblies from 3.5 directory). Depending on your interpretation of the term ".NET Framework directory path", GetRuntimeDirectory might not be the information you are looking for ("CLR directory" versus "directory from which 3.5 assemblies are coming from").

另一个重要的事实是,对于 .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 程序集来自的目录”)。

回答by CMS

You can grab it from the Windows Registry:

您可以从 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

来源

回答by Ihar Voitka

Read value of the [HKLM]\Software\Microsoft.NetFramework\InstallRootkey - you will get "C:\WINDOWS\Microsoft.NET\Framework". Then append with desired framework version.

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

回答by Brian Rudolph

An easier way is to include the Microsoft.Build.Utilities assembly and use

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

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