Finding the .NET Framework library path

Security Briefs

Syndication

I recently had a need to find the path to the .NET Framework binaries. Here's the official way of finding it, using a function exposed from mscoree.dll:

const int MAX_PATH = 256;
public string GetNetFrameworkDirectory() {
    StringBuilder buf = new StringBuilder(
        MAX_PATH, MAX_PATH);
    int cch = MAX_PATH;
    int hr = GetCORSystemDirectory(
        buf, MAX_PATH, ref cch);
    if (hr < 0) Marshal.ThrowExceptionForHR(hr);
    return buf.ToString();
}
[DllImport("mscoree.dll",
         CharSet=CharSet.Unicode,
         ExactSpelling=true)]
public static extern int GetCORSystemDirectory(
        StringBuilder buf,
        int cchBuf,
        ref int cchRequired);

This returned the following string on my 1.1 box:

C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\


Posted Oct 18 2005, 02:33 PM by keith-brown
Filed under:

Comments

Mike wrote re: Finding the .NET Framework library path
on 10-18-2005 2:54 PM
I've always just referenced System.Web.dll and then read the static HttpRuntime.ClrInstallDirectory property. It's documented, involves less typing, and it saves me the hassle of having to have enough permissions to use p/invoke. But then, I'm lazy at my core :-)
Keith Brown wrote re: Finding the .NET Framework library path
on 10-18-2005 3:01 PM
Ahh, I like that even better. Thanks for the reference, Mike.
Stephen Toub wrote re: Finding the .NET Framework library path
on 10-18-2005 5:03 PM
Or you can use System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
Security Briefs wrote The official *managed* way to find the .NET Framework directory
on 10-18-2005 7:41 PM
Security Briefs wrote The official *managed* way to find the .NET Framework directory
on 10-18-2005 7:41 PM
Dev Emporium wrote How to find the .NET Framework directory
on 10-24-2005 10:40 AM
Keith Brown had two posts recently detailing how to get the path to the .NET Framework directory on a...
Javier G. Lozano wrote Finding The .NET Framework Path
on 05-16-2006 10:10 PM
Javier G. Lozano wrote Finding The .NET Framework Path
on 05-17-2006 11:11 AM
Mohsen wrote re: Finding the .NET Framework library path
on 01-15-2007 5:37 PM
RuntimeEnvironment.GetRuntimeDirectory()