Determining Framework SDK Path

Share on:

Something I’ve needed to do a couple of times now is programmatically work out where the the Framework SDK is installed. Each time I have to find an example from an old project, because I can never remember exactly how I did it before, so for posterity (and to save me searching again), I’m posting it up here.

Feel free to use this particular snippet however you want, but as usual, I don’t provide any warranties that it fit for purpose, etc, so use it at your own risk! Note that there’s no error handling for when the SDK is not installed, so you might want to add that in if you’re intending on using it in a fault-tolerant environment.

 1
 2private static string sdkLocation;
 3
 4private static string SdkLocation
 5{
 6    get
 7    {
 8        if (sdkLocation == null)
 9        {
10            sdkLocation = String.Empty;
11
12            RegistryKey key = Registry.LocalMachine.OpenSubKey(
13                 @"SOFTWARE\Microsoft\.NETFramework");
14            if (key != null)
15            {
16                object location = key.GetValue("sdkInstallRoot" +
17                     ExecutingFrameworkVersion);
18                if (location != null)
19                {
20                    sdkLocation = Path.Combine(location.ToString(),
21                        "Bin");
22                }
23            }
24        }
25
26        return sdkLocation;
27    }
28}
29
30private static string ExecutingFrameworkVersion
31{
32    get
33    {
34        return RuntimeEnvironment.GetSystemVersion().Substring(0, 4);
35    }
36}