Retrieve the current Framework Directory
I needed the ablity to get the directory of the most current version of the framework on them machine, but there were no built in functions in the framework to do this (at least none that I could find). So I did a little hunting around and found where the framework installation path is stored in the registry and combined that with the Environment.Version to get the framework directory. Here is a sample function to retrieve the framework directory:
using System; using Microsoft.Win32;
...
////// This method will retrieve the directory of the framework that this /// program is executing under. It accomplishes this by getting the /// install root directory from the registry and then getting the framework /// version by using Environment.Version. /// /// path of the framework executing this program 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); }