2

I am using below code to get installed date of my windows application. My application was installed using installer and it's listed in control panel as well. However, below code doesn't get me my application details. Any ideas?

[DllImport("msi.dll", CharSet = CharSet.Unicode)]
static extern Int32 MsiGetProductInfo(string product, string property,
    [Out] StringBuilder valueBuf, ref Int32 len);

[DllImport("msi.dll", SetLastError = true)]
static extern int MsiEnumProducts(int iProductIndex,
    StringBuilder lpProductBuf);

static void Main(string[] args)
{
    StringBuilder sbProductCode = new StringBuilder(39);
    int iIdx = 0;
    while (
        0 == MsiEnumProducts(iIdx++, sbProductCode))
    {
        Int32 productNameLen = 512;
        StringBuilder sbProductName = new StringBuilder(productNameLen);

        MsiGetProductInfo(sbProductCode.ToString(),
            "ProductName", sbProductName, ref productNameLen);

        if (sbProductName.ToString().Contains("MyApplication"))
        {
            Int32 installDirLen = 1024;
            StringBuilder sbInstallDir = new StringBuilder(installDirLen);
            MsiGetProductInfo(sbProductCode.ToString(),
                "InstallLocation", sbInstallDir, ref installDirLen);

            if (sbInstallDir.ToString() != "")
            {
                Console.WriteLine("ProductName {0}: {1}",
                    sbProductName, GetCreationDateOfFolder(sbInstallDir.ToString()).ToString());
            }
        }
    }
}

private static DateTime GetCreationDateOfFolder(string Path)
{
    string directoryString = Path;
    Directory.CreateDirectory(directoryString);
    DateTime dateTime = Directory.GetCreationTime(directoryString);
    return dateTime;
}
codematrix
  • 1,571
  • 11
  • 35
  • 53
  • The details are in the registry. However, it seems that your application name has changed since last install or you don't have access to run the msi functions. – Ross Bush Apr 30 '14 at 02:00
  • I don't want to get from registry! I need to access 64 bit registry from 32 bit application. My changes are not working! So, I switched to this approach. – codematrix Apr 30 '14 at 02:04
  • Have you looked into using the Wow6432Node? Anyway, the above approach seems cleaner. I don't know why its failing unless there is an access denied error. – Ross Bush Apr 30 '14 at 02:07
  • I tried Wow6432Node as well. My application is listed in Control Panel. Are there any other requirements for above code to work? – codematrix Apr 30 '14 at 02:17
  • Was your app installed via Windows Installer (e.g. by an MSI)? [Just because your app was installed with an installer and it is listed in the Control Panel does not mean that it was installed via Windows Installer](http://stackoverflow.com/q/3526449/1810429). – J0e3gan Apr 30 '14 at 02:28
  • I am facing same problem. My application uses Inno setup. can this be a problem? – deathrace Oct 30 '17 at 10:25
  • @J0e3gan is a .exe definitely not a Windows Installer? my installer is .exe and it's generated by InstallShield, its product name cannot be retrieved by MsiGetProductInfo either – user1108069 Oct 21 '22 at 04:41

0 Answers0