79

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"

Csharp
  • 2,916
  • 16
  • 50
  • 77
csfb
  • 1,105
  • 1
  • 9
  • 19

5 Answers5

185

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

Milan Gardian
  • 11,326
  • 5
  • 38
  • 45
  • 2
    I believe this is the right answer and should be the selected one. Thanks for clarifying the fact that GetRuntimeDirectory always returns the 2.0 folder even on 3.0 or 3.5 apps. This is the correct behavior in most cases, where you want to access the framework tools, which are in 2.0 (but not in 3.0 3.5). – dso May 01 '09 at 08:44
  • How can I get InstallRoot for x86 and x64 .NET frameworks on 64bit systems? Is "[HKLM]\Software\Microsoft.NetFramework\InstallRoot" always pointing to x86 version of .NET, even on 64bit systems? I need to get path to this folder with non-managed application, so I can't use the method you listed above. Thanks. – Paya Dec 30 '10 at 21:14
  • 1
    Probably the easiest would be to create tiny .NET applications, one compiled for x86 (e.g. 'getdotnetpath32.exe') and another compiled for x64 (e.g. 'getdotnetpath64.exe'). The managed application would use the GetRuntimeDirectory() call and write it to STDOUT (Console.Output). Unmanaged application would then start a child process for x86 (getdotnetpath32.exe), connecting its STDOUT to its in-memory stream and reading what the process produces. Then it would start a child process for x64 (getdotnetpath64.exe), connecting its STDOUT to its in-memory stream and reading what the process produces – Milan Gardian Jan 10 '11 at 23:28
  • 1
    If you would like the path to the directory where configuration files are stored, you can do the following: var readFromDirectory = System.IO.Path.GetDirectoryName(System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile); var mediumTrustConfigPath = System.IO.Path.Combine(readFromDirectory, "web_mediumtrust.config"); – Alex Norcliffe Oct 17 '11 at 13:43
44

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

using Microsoft.Build.Utilities;
ToolLocationHelper.GetPathToDotNetFramework(
        TargetDotNetFrameworkVersion.VersionLatest);
Brian Rudolph
  • 6,142
  • 2
  • 23
  • 19
2

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

Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 12
    I strongly advice against accessing registry directly (on a 64bit operating system this may actually give wrong answer). See my answer below for details. – Milan Gardian May 01 '09 at 12:20
  • @CMS i want to do same like silvetlight version . and if i am delete registry key that means software also uninstall from the system . Thank you – Dhru 'soni Jan 29 '15 at 12:33
0

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();
        }
    }
}
ezolotko
  • 1,723
  • 1
  • 21
  • 21
-3

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

Ihar Voitka
  • 549
  • 3
  • 8