<November 2018>
MoTuWeThFrSaSu
2930311234
567891011
12131415161718
19202122232425
262728293012
3456789
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