如何阅读在C#MSI属性 [英] How to read MSI properties in c#

查看:457
本文介绍了如何阅读在C#MSI属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读桌面application.I在C#MSI的性能我用下面的代码:

I want to read properties of MSI in C# in desktop application.I am using following code:

    public static string GetMSIProperty( string msiFile, string msiProperty)
    {
        string retVal= string.Empty ;

        Type classType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
        Object installerObj = Activator.CreateInstance(classType);
        WindowsInstaller.Installer installer = installerObj as WindowsInstaller.Installer;

        Database database = installer.OpenDatabase("C:\\DataP\\sqlncli.msi",0 );   

        string sql = String.Format("SELECT Value FROM Property WHERE Property=’{0}’", msiProperty);

        View view = database.OpenView(sql);

        Record record = view.Fetch();

        if (record != null)
        {
            retVal = record.get_StringData(1);
        }
        else
            retVal = "Property Not Found";

        return retVal;            
    }



但我得到错误的System.Runtime.InteropServices.COMException了未处理。

But I am getting error as System.Runtime.InteropServices.COMException was unhandled.

sqlncli.msi文件物理放置在C:\DataP位置。虽然调试我发现数据库不包含后installer.OpenDatabase()语句中的数据。

the sqlncli.msi file is physically placed at c:\DataP location. While debugging I found that database does not contain the data after installer.OpenDatabase() statement.

请建议我如何解决这个问题,在C#中得到MSI属性。

Please suggest me how to resolve this issue and get MSI properties in c#.

在此先感谢。

推荐答案

的Windows Installer XML的部署工具基金会(维克斯DTF)是微软一个开源项目,其中包括了Microsoft.Deployment.WindowsInstaller MSI互操作库。它更容易和更可靠使用该做这类查询。它甚至有一个LINQ到MSI提供商,可以让你把MSI表作为实体写对他们的查询

Windows Installer XML's Deployment Tools Foundation (WiX DTF) is an Open Source project from Microsoft which includes the Microsoft.Deployment.WindowsInstaller MSI interop library. It's far easier and more reliable to use this to do these sorts of queries. It even has a LINQ to MSI provider that allows you to treat MSI tables as entities and write queries against them.

using System;
using System.Linq;
using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Deployment.WindowsInstaller.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using(var database = new QDatabase(@"C:\tfs\iswix.msi", DatabaseOpenMode.ReadOnly))
            {
                var properties = from p in database.Properties
                                 select p;

                foreach (var property in properties)
                {
                    Console.WriteLine("{0} = {1}", property.Property, property.Value);
                }
            }

            using (var database = new Database(@"C:\tfs\iswix.msi", DatabaseOpenMode.ReadOnly))
            {
                using(var view = database.OpenView(database.Tables["Property"].SqlSelectString))
                {
                    view.Execute();
                    foreach (var rec in view) using (rec)
                    {
                        Console.WriteLine("{0} = {1}", rec.GetString("Property"), rec.GetString("Value"));
                    }
                }
            }

            Console.Read();
        }
    }
}

这篇关于如何阅读在C#MSI属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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