获取 .NET Framework 目录路径 [英] Getting the .NET Framework directory path

查看:122
本文介绍了获取 .NET Framework 目录路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我指的文件夹是C:WINDOWSMicrosoft.NETFrameworkv2.0.50727"

The folder that I refer is "C:WINDOWSMicrosoft.NETFrameworkv2.0.50727"

推荐答案

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

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()

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

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:WindowsMicrosoft.NETFramework64v2.0.50727" (AnyCPU, x64 compilation targets) or from "C:WindowsMicrosoft.NETFrameworkv2.0.50727" (x86 compilation target). Reading registry will not tell you which one of the two directories was used by the current 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 程序集来自的目录").

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 Framework 目录路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆