<May 2023>
MoTuWeThFrSaSu
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
Getting .Net Versions Installed from C#


Here is a method to list all .Net versions installed on system in C#. I was surprised that I couldn't find single line solution for this. Of course, solution needs to work on lowest possible framework -this one requires .Net 2.0.

public static List GetDotNetVersionsInstalledFromRegistry()
{
	List r = new List();
	using (RegistryKey ndpKey = RegistryKey
		.OpenRemoteBaseKey(RegistryHive.LocalMachine, string.Empty)
		.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
	{
		foreach (string versionKeyName in ndpKey.GetSubKeyNames())
		{
			if (versionKeyName.StartsWith("v"))
			{

				RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName);
				string name = (string)versionKey.GetValue("Version", "");
				string sp = versionKey.GetValue("SP", "").ToString();
				string install = versionKey.GetValue("Install", "").ToString();
				if (install == "")
				{
					//no install info, ust be later
					r.Add(versionKeyName + "," + name);
				}
				else
				{
					if (sp != "" && install == "1")
					{
						r.Add(versionKeyName + "," + name + ",SP" + sp);
					}
				}
				if (name != "")
				{
					continue;
				}
				foreach (string subKeyName in versionKey.GetSubKeyNames())
				{
					RegistryKey subKey = versionKey.OpenSubKey(subKeyName);
					name = (string)subKey.GetValue("Version", "");
					if (name != "")
						sp = subKey.GetValue("SP", "").ToString();
					install = subKey.GetValue("Install", "").ToString();
					if (install == "")
					{
						//no install info, ust be later
						r.Add(versionKeyName + "," + name);
					}
					else
					{
						if (sp != "" && install == "1")
						{
							r.Add(versionKeyName + "," + 
								subKeyName + "," + name + ",SP" + sp);
						}
						else if (install == "1")
						{
							r.Add(versionKeyName + "," + 
								subKeyName + "," + name);
						}

					}

				}

			}
		}
	}
	return r;
}

.
No Comments

Title
Author
Comment
Anti Bot Image   
  

Comments

Name
E-Mail@
(optional; never shown publicly; for notifications from this thread)
Comment
Anti Bot Image