none
How to obtain Product Code within a C# application?

    Question

  • I have a Visual Studio 2005 C# application which is a Windows Forms application.  I want to know if there is a method in the .NET 2.0 framework to obtain the product code of the currently running application.  This is needed in order to find the Uninstall registry key for the application.  I could just iterate through the keys under the Uninstall key until I find the one that matches my application, but that seems inefficient. I can't just hard-code the location of the registry key because it changes with each program update.  The Application class gives some information including the application version, but doesn't seem to contain the product code.

     

    The reason I want to do this, by the way, is that installing program updates sometimes creates problems.. the MSI file is configured to silently remove previous versions of the application, however sometimes errors result, and it becomes necessary for the user to manually uninstall before installing the new version.  I would like to have an update script which would invoke the uninstall string with the silent option and then launch the MSI for the new version.

     

    Thanks.

    Thursday, June 12, 2008 8:37 PM

Answers

  •  

    Hi Lewis,

    Since the msi package was created by yourself, I would recommend you save the product code special registry entry (hard code), and then you can read the product code from the registry entry in your application. It is very easy to handle. You just need to set Value of the hard coded registry entry to [ProductCode].

    If you don’t want to change your msi package, you can read the product code from WMI (Win32_Product Class). You will need to loop through all the product that have been installed the client computer and check the name of the product to get the product code of your application.

    Code Snippet

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");

                string productcode;

                foreach (ManagementObject wmi in searcher.Get())

                {

                    if (wmi.GetPropertyValue("Name").ToString() == "yourapplicationname")

                    {

                        productcode = wmi.Properties["ProductID"].Value.ToString();

                        break;

                    }

                }

     

    For more samples about how to use WMI, please check:
    http://www.codeproject.com/KB/system/WMIcalls.aspx
    http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx

    If you are using VB.NET, you can use COM object to get the Produce for all installed applications. Try something like the following:

    Code Snippet

            Dim installer As Object

            installer = CreateObject("WindowsInstaller.Installer")

            Dim products As Object = installer.Products

     

            For Each strProductCode As String In products

                'Console.WriteLine(strProductCode)

                Dim strProductName As String = installer.ProductInfo(strProductCode, "InstalledProductName")

                If strProductName = "Something" Then

                    'That's it.

                End If

            Next

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Wednesday, June 18, 2008 10:44 AM

All replies

  • I'm not exactly sure what you mean by "product code".  If it is the AssemblyProduct attribute used in AssemblyInfo.cs, you can retrieve it with this code:

    using System.Reflection;
    ....
          Assembly main = Assembly.GetEntryAssembly();
          AssemblyProductAttribute attr = (AssemblyProductAttribute)
            main.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0];
          Console.WriteLine(attr.Product);
    Saturday, June 14, 2008 12:26 AM
    Moderator
  •  nobugz wrote:
    I'm not exactly sure what you mean by "product code".  If it is the AssemblyProduct attribute used in AssemblyInfo.cs, you can retrieve it with this code:

     

    I'm sorry I wasn't clear.  What I have in mind is the ProductCode attribute which is defined in the MSI file used to install the application.  It is a sequence of, maybe, thirty or forty letters and digits separated by several dashes and enclosed in curly braces.  As I mentioned, I need this value in order to find the subkey for my application under the Uninstall key in the Registry, unless I just iterate through all the subkeys looking for the one that has my application name.

     

    Thanks

    Lewis

    Monday, June 16, 2008 1:58 PM
  •  

    Hi Lewis,

    Since the msi package was created by yourself, I would recommend you save the product code special registry entry (hard code), and then you can read the product code from the registry entry in your application. It is very easy to handle. You just need to set Value of the hard coded registry entry to [ProductCode].

    If you don’t want to change your msi package, you can read the product code from WMI (Win32_Product Class). You will need to loop through all the product that have been installed the client computer and check the name of the product to get the product code of your application.

    Code Snippet

                ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");

                string productcode;

                foreach (ManagementObject wmi in searcher.Get())

                {

                    if (wmi.GetPropertyValue("Name").ToString() == "yourapplicationname")

                    {

                        productcode = wmi.Properties["ProductID"].Value.ToString();

                        break;

                    }

                }

     

    For more samples about how to use WMI, please check:
    http://www.codeproject.com/KB/system/WMIcalls.aspx
    http://www.codeproject.com/KB/cs/EverythingInWmi03.aspx

    If you are using VB.NET, you can use COM object to get the Produce for all installed applications. Try something like the following:

    Code Snippet

            Dim installer As Object

            installer = CreateObject("WindowsInstaller.Installer")

            Dim products As Object = installer.Products

     

            For Each strProductCode As String In products

                'Console.WriteLine(strProductCode)

                Dim strProductName As String = installer.ProductInfo(strProductCode, "InstalledProductName")

                If strProductName = "Something" Then

                    'That's it.

                End If

            Next

     

    Hope this helps.
    Best regards.
    Rong-Chun Zhang

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

    Wednesday, June 18, 2008 10:44 AM
  •  

    Hi misters,

     

    How I can do this in C# ??

     

    Dim installer As Object

            installer = CreateObject("WindowsInstaller.Installer")

            Dim products As Object = installer.Products

     

     

    Thanks !!!

    Friday, September 26, 2008 7:03 AM
  • You can do something like this:

    After creating the install package you can get the properties which in my case returned in object[] for further use.

      public object[] GetFilesFromMSI()
            {
                object[] odRet = new object[3];
                InstallPackage InstPakage = new InstallPackage(SMsiPath,
                                                               Microsoft.Deployment.WindowsInstaller.DatabaseOpenMode.
                                                                   ReadOnly, null, SworkDir);
                InstPakage.ExtractFiles();
                odRet[0] = InstPakage.Property["ProductVersion"];
                odRet[1] = InstPakage.Property["UpgradeCode"];
                odRet[2] = InstPakage.Property["ProductCode"];
                return odRet;
            }


    • Edited by Lazy Jack Friday, April 06, 2012 2:01 PM
    Friday, April 06, 2012 2:00 PM
  • 轻松挂机赚钱,180元/天,无需任何投资,像QQ一样挂着就能赚钱!免费加入!
    Sunday, April 08, 2012 12:18 AM