Hi Chuck,
I agree with you that the following statement will return the path of the
CLR that currently runs the assembly, which maybe not the latest version of
CLR installed on the machine:
string location
=System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory();
NET Framework with different versions can be installed side by side on a
machine. When a .NET program runs, it will use the same version of CLR it
is built on, only if this version of CLR has been installed. So it's not
possible to detect the latest version of CLR in a .NET program using any
existing .NET class related to the CLR.
So we must do it by other means.
When we install .NET Framework on a machine, some information is recorded
in the registry. Type "regedit" in the run prompt to open registry editor.
Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework. You will
see an item named "InstallRoot", which records a general path of CLR. The
value of this item on my machine is "C:\WINNT\Microsoft.NET\Framework".
Unfortunately, there is not a value in the registry that contains the path
of the latest version of CLR. But all versions of installed CLR are listed
under the "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\" branch as
sub keys, such as v2.0.50727 and v3.0. Thus, we could get the latest one
and add it to the intall root to get the path of the latest version of CLR.
The following is a sample.
using Microsoft.Win32;
private string GetInstallRoot()
{
RegistryKey rk = Registry.LocalMachine;
rk = rk.OpenSubKey("software\\microsoft\\.NetFramework",false);
string installRoot = rk.GetValue("InstallRoot").ToString();
return installRoot;
}
private string GetLatestVersion()
{
// Create a RegistryKey, which will access the
HKEY_LOCAL_MACHINE
// key in the registry of this machine.
RegistryKey rk = Registry.LocalMachine;
rk = rk.OpenSubKey("software\\microsoft\\.NetFramework",false);
// Retrieve all the subkeys for the specified key.
String[] names = rk.GetSubKeyNames();
string latestVersion = "";
foreach (String name in names)
{
if (name.StartsWith("v") && name.CompareTo(latestVersion) >
0)
latestVersion = name;
}
return latestVersion;
}
Then you could get the path of the latest version of CLR using the
following code.
string path = GetInstallRoot() + GetLatestVersion();
Hope this helps.
If you have any concerns, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.