19.5. Finding the Path to the Current Framework Version
Problem
You need the path to where the version of the .NET Framework you are running on is located.
Solution
Use the GetRuntimeDirectoryRuntimeDirectory method (implemented in System. Runtime.InteropServices.RuntimeEnvironment) to return the full path to the folder that the current version of .NET is installed in:
public static string GetCurrentFrameworkPath()
{
return
System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
}Discussion
There are many reasons why you might want to know the current framework path, including:
Manually loading the configuration files in the config directory to check settings.
Dynamically adding references for system components in a code generator.
The list could go on and on. Since the method to get to the path is pretty far down a namespace chain (System.Runtime.InteropServices.RuntimeEnvironment), it is provided for your programming convenience.
See Also
The "Version Class" and "Version.ToString Method" topics in the MSDN documentation.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access