Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the .NET Framework directory path

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"

like image 599
Bruno Cartaxo Avatar asked Dec 17 '08 20:12

Bruno Cartaxo


People also ask

How do I find the .NET framework path?

You can check your installed versions of . NET by navigating to Microsoft.NET\Framework under your Windows folders. The complete path is usually 'C:\Windows\Microsoft.NET\Framework.

How do I access .NET framework?

Select Start > Control Panel > Programs > Programs and Features. Select Turn Windows features on or off. If not already installed, select Microsoft . NET Framework and click OK.

Where is .NET Framework SDK installed?

The values beneath HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\SxS\FRAMEWORKSDK tell you where the SDK is installed. If there's a 7.1 value, you know where the . NET 1.1 Framework SDK is installed.

Where is .NET 3.5 installed?

NET 3.5 is installed by looking at HKLM\Software\Microsoft\NET Framework Setup\NDP\v3.


2 Answers

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").

like image 140
Milan Gardian Avatar answered Sep 21 '22 20:09

Milan Gardian


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

using Microsoft.Build.Utilities; ToolLocationHelper.GetPathToDotNetFramework(         TargetDotNetFrameworkVersion.VersionLatest); 
like image 25
Brian Rudolph Avatar answered Sep 21 '22 20:09

Brian Rudolph