MsiGetProductInfo不会向我提供我的产品信息

时间:2014-04-30 01:51:49

标签: c#

我使用下面的代码来获取我的Windows应用程序的安装日期。我的应用程序是使用安装程序安装的,它也在控制面板中列出。但是,下面的代码没有得到我的应用程序详细信息。有什么想法吗?

[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;
}

0 个答案:

没有答案