0

好的,所以我有以下用 C# 编写的类库:

public class Program
{
    public void GetProductID(string location, out string productId)
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_Product");
        ManagementObjectCollection collection = searcher.Get();
        var item = new Win32Product();
        //var crap = (collection as IEnumerable<ManagementObject>).Where(o => o["InstallLocation"].ToString().StartsWith(location));
        foreach (ManagementObject obj in collection)
        {
            try
            {
                item = new Win32Product();
                item.IdentifyingNumber = (string)obj["IdentifyingNumber"];
                item.InstallLocation = (string)obj["InstallLocation"];
                item.Name = (string)obj["Name"];
            }
            catch
            { }  //shut up. I know it's an empty catch block. Its fine.
                 //If there are exceptions thrown, I don't want the data, I just
                 //want to keep running.
        }
        productId = item.ProductID.ToString();
    }        
} 

public class Win32Product
{
   //properties
}

不是很多,GetProductId()只是搜索给定目录下的任何已安装程序。如果我在其他地方对其进行测试,它可以正常工作,但是从 installshield 运行(作为控制事件)时,它会失败(返回值 3)。

我猜这是一个模糊的问题,但知道为什么GetProductInfo()installshield 会失败吗?

4

1 回答 1

2

这里有一些阅读材料给你:

部署工具基础 (DTF) 托管的自定义操作

DTF 更好的原因

顺便说一句,Win32_Product 类是一个 POS。它在包装底层 MSI API 方面做得很差。当您切换到 DTF 时,请改用 ProductInstallation::GetProducts 方法。调用 MsiEnumProductsEx 做得更好。

于 2012-09-18T21:19:32.967 回答