获取 .NET Framework 目录路径

发布于 2024-07-10 16:29:21 字数 107 浏览 5 评论 0原文

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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等往事风中吹 2024-07-17 16:29:21

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

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

强烈建议不要直接读取注册表。 例如,当 .NET 应用程序在 64 位系统中运行时,可以从“C:\Windows\Microsoft.NET\Framework64\v2.0.50727”(任何 CPU、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 程序集所在的目录”)。

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

System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

I would strongly advice 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 not tell you which one of the two directories was used by the current 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").

溺孤伤于心 2024-07-17 16:29:21

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

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

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

using Microsoft.Build.Utilities;
ToolLocationHelper.GetPathToDotNetFramework(
        TargetDotNetFrameworkVersion.VersionLatest);
ゃ人海孤独症 2024-07-17 16:29:21

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

来源

You can grab it from the Windows Registry:

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

萌逼全场 2024-07-17 16:29:21

对于 .NET Framework 版本 >= 4.5,来自 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();
        }
    }
}

For .NET Framework versions >= 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();
        }
    }
}
孤君无依 2024-07-17 16:29:21

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文