Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Get Version of a Software using C#

4.44/5 (7 votes)
29 Dec 2013CPOL 35.9K   728  
Get Version of any software on MSI installed in any System/Server

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

C#
//Download the "ReadSoftwareVersion.dll" from the attachments and add it to the references of your project 
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();
        }
    }
}
// This is a Wild Card Search. You need not give full name for "Your Software Name" 

Actual Code inside the "ReadSoftwareVersion.dll" 

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;  // Include this in references

namespace ReadSoftwareVersion
{
    // your own class name
    public class Software
    {
        // Method that will fetch the version of a given software
        public string GetSoftwateVersion(string softWareName)
        {
            string strVersion = string.Empty;
            try
            {
                var version = (object)null;
                //Query the system registery for the verion of the given software                
                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;
                }
                // if given product is not found in list of installed products in control panel
                else
                {
                    strVersion = "Given Product is not found the list of Installed Programs";
                }
            }
            // Exception Handling
            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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)