Introduction
This DLL will get you the Version of the software that is present in your installed programs in control panel. This is specially use full when we might need to get the MSI version of any product that we drop in servers.
Using the code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReadSoftwareVersion;
namespace TestVersion
{
class Program
{
static void Main(string[] args)
{
Software objVersion = new Software();
string version = objVersion.GetSoftwateVersion("Your Software Name");
Console.WriteLine(version);
Console.ReadLine();
}
}
}
Actual Code inside the "ReadSoftwareVersion.dll"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ReadSoftwareVersion
{
public class Software
{
public string GetSoftwateVersion(string softWareName)
{
string strVersion = string.Empty;
try
{
var version = (object)null;
var searcher = new ManagementObjectSearcher(
"SELECT * FROM Win32_Product where Name LIKE " +
"'%" + softWareName + "%'");
foreach (ManagementObject obj in searcher.Get())
{
version = obj["Version"];
}
if (version != null)
{
strVersion = (String)version;
}
else
{
strVersion = "Given Product is not found the list of Installed Programs";
}
}
catch (Exception e)
{
strVersion = "An Error occured while fetching Version" +
" (" + e.Message + ")";
}
return strVersion;
}
}
}
Here just a Console Application is shown for DEMO. In general, this dll can be added to any project, i.e to web application.... where ever needed.
Points of Interest
This will specially help in the cases where we need to fetch the MSI information of the product that is dropped in remote server. Generally, we might need to follow with installation
teams to get the MSI Version. But using this we can fetch the MSI Version and display it in UI, if needed.
This method is relatively slow and will take around 10 to 20 seconds to execute. This is completely tested and 100% working.