为什么在同时使用DTF和MSI API的C#中,vb能够找到INSTALLLOCATION? [英] Why is vbs able to find the INSTALLLOCATION when C# using both DTF and MSI API cannot?

查看:91
本文介绍了为什么在同时使用DTF和MSI API的C#中,vb能够找到INSTALLLOCATION?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

VBS可以按我的要求工作,但是使用C#的COM API和DTF都没有找到InstallLocation.以下是我到目前为止所做的事情.

VBS works as I desired, but both COM API and DTF using C# is not locating the InstallLocation. Followings are what I have done so far.

由于这篇文章,我能够找到使用vbs在注册表上不可用的InstallLocation.我了解vbs正在调用%WINDIR%\system32\msi.dll上可用的COM API.

Thanks to this post, I was able to find a InstallLocation that is not available on registry using vbs. I understand that vbs is calling for COM API available on %WINDIR%\system32\msi.dll.

所以我想我会用C#调用此方法.但是失败了.即使我可以确认存在和已安装,也无法打开其中一个产品GUID(我已三重检查).

So I thought I would use C# to call this method up. But it failed. Even though I can confirm the existence and installation, it cannot open one of the product GUID (I tripple checked).

注意:有些产品没有引发异常,并且可以正确找到InstallLocation.并非全部.

以下是我的代码.

        static Dictionary<string, string> FindInstallLocationsCOM(Dictionary<string, string> products)
        {
            var locationDictionary = new Dictionary<string, string>();

            // Get the type of the Windows Installer object
            Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");

            // Create the Windows Installer object
            Object installerObj = Activator.CreateInstance(installerType);
            Installer installer = installerObj as Installer;

            foreach (var product in products)
            {
                try
                {
                    var session = installer.OpenProduct(product.Value);
                    if (session != null)
                    {
                        session.DoAction("CostInitialize");
                        session.DoAction("CostFinalize");
                        var installLocation = session.Property["INSTALLLOCATION"];
                        MessageBox.Show(product.Key + "\n" + "Product Code : " + product.Value + "\n" + "Install Location : " + installLocation);
                        locationDictionary.Add(product.Key, installLocation);
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error : Could not open Product " + e.Message + "\n" + "Product : " + product.Key + "\n" + "Product Code : " + product.Value);
                }
            }

            return locationDictionary;
        }

行不通,让我们尝试DTF.

OK that did not work, let's try DTF.

但是那也不成功.以下是我的代码.这不会触发异常,即使是无法通过COM API检测到的异常也能够自我检测,但是InstallLocation属性为空字符串.

But that also was not successful. Following is my code. This does not trigger exception, and even the one that was not detectable via COM API was able to detect itself, but InstallLocation property was empty string.

注意:有些产品确实具有InstallLocation属性.并非全部.

        static Dictionary<string,string> FindInstallLocation(Dictionary<string,string> products)
        {
            var locationDictionary = new Dictionary<string, string>();

            foreach (var product in products)
            {
                try
                {
                    var installed = new ProductInstallation(product.Value);
                    if (installed != null)
                    {
                        var installLocation = installed.InstallLocation;
                        MessageBox.Show(product.Key + "\n" + "Product Code : " + product.Value + "\n" + "Install Location : " + installLocation);
                        locationDictionary.Add(product.Key, installLocation);
                    }
                    else
                    {
                        MessageBox.Show(product.Key + "\n" + "Product Code : " + product.Value + "\n" + "Is not installed");
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("Error :  " + e.Message + "\n" + "Product : " + product.Key + "\n" + "Product Code : " + product.Value);
                }
            }

            return locationDictionary;
        }

为什么在C#都无法执行的情况下VBS能够检测到InstallLocation?我想念什么?

Why is VBS able to detect the InstallLocation when neither of C# is not able to? What am I missing?

我无法使用VBS的原因是,除非使用vb.net,否则无法使用try catch.

The reason I cannot use VBS is because the try catch is not available unless I use vb.net.

推荐答案

在SteinAsmul提出DTF不会自动调用与费用相关的操作后,我进一步阅读了DTF文档.

After SteinAsmul's suggesion that DTF does not automatically call cost related action, I did further reading in the DTF document.

我发现DTF中也提供了DoAction.因此,我使用了以下内容,并且var installLocation现在具有我想要的期望值.

I found an DoAction is also available in DTF. So I used the following, and the var installLocation now has the expected value I was looking for.

Installer.SetInternalUI(InstallUIOptions.Silent);
var session = Installer.OpenProduct(product.Value);
session.DoAction("CostInitialize");
session.DoAction("CostFinalize");
var installLocation = session["INSTALLLOCATION"];
session.Close();

这篇关于为什么在同时使用DTF和MSI API的C#中,vb能够找到INSTALLLOCATION?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆