我可以从C ++或C#程序中提取MSI软件包的内容吗? [英] Can I extract contents of MSI package from within C++ or C# program?

查看:117
本文介绍了我可以从C ++或C#程序中提取MSI软件包的内容吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说,如果我有MSI安装文件,是否可以不安装而从C#或C ++程序中提取其内容?

Say, if I have an MSI installation file, can I extract its contents from a C# or C++ program without installing it?

推荐答案

通常,您可以执行管理安装提取MSI的内容。

Typically you can perforrm an Administrative installation to extract the contents of an MSI.

msiexec /a foo.msi TARGETDIR=C:\EXTRACTHERE /qn

如果您不想退出流程,可以直接与MSI互操作通过 MsiInstallProduct函数

If you don't want to go out of process you can interop directly with MSI via the MsiInstallProduct function.

szPackagePath [in]以空值结尾的字符串,它指定Windows Installer程序包位置的路径。字符串值可以包含URL,网络路径,文件路径(例如file://packageLocation/package.msi)或本地路径(例如D:\packageLocation\package.msi)。

szPackagePath [in] A null-terminated string that specifies the path to the location of the Windows Installer package. The string value can contain a URL a network path, a file path (e.g. file://packageLocation/package.msi), or a local path (e.g. D:\packageLocation\package.msi).

szCommandLine [in]以空值结尾的字符串,用于指定命令行属性设置。这应该是格式为Property = Setting Property = Setting的列表。有关更多信息,请参见关于属性。

szCommandLine [in] A null-terminated string that specifies the command line property settings. This should be a list of the format Property=Setting Property=Setting. For more information, see About Properties.

要执行管理安装,请在szCommandLine中包含ACTION = ADMIN。有关更多信息,请参见ACTION属性。

To perform an administrative installation, include ACTION=ADMIN in szCommandLine. For more information, see the ACTION property.

请注意,尽管您可以自行声明P / Invoke,但称为部署工具基础(DTF)的Windows Instaler XML 。 Microsoft.Deployment.WindowsInstaller命名空间具有一个称为Installer的类方法,该方法公开一个称为InstallProduct的静态方法。这是MsiInstallProduct的直接封装。

Note that while you can declare the P/Invoke yourself, there is a really good .NET interop library available with Windows Instaler XML called Deployment Tools Foundation (DTF). The Microsoft.Deployment.WindowsInstaller namespace has a class method called Installer that exposes a static method called InstallProduct. This is a direct encapsulation of MsiInstallProduct.

使用DTF库可以使您免受Win32 API的丑陋影响,并在需要的地方正确实现IDisposable,以确保释放基础的非托管句柄。

Using the DTF libraries hides you from the ugliness on the Win32 API and correctly implements IDisposable where needed to ensure that underlying unmanaged handles are released where needed.

此外,DTF还具有Microsoft.DeploymentWindowwsInstaller.Package命名空间和InstallPackage类。此类提供了一个称为ExtractFiles()的方法,该方法将文件提取到工作目录中。代码示例如下:

Additionally DTF has the Microsoft.DeploymentWindowwsInstaller.Package namespace with the InstallPackage class. This class exposes a method called ExtractFiles() that extracts the files to the working directory. An example of code looks like:

using Microsoft.Deployment.WindowsInstaller;
using Microsoft.Deployment.WindowsInstaller.Package;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using( var package = new InstallPackage(@"C:\test.msi", DatabaseOpenMode.ReadOnly))
            {
                package.ExtractFiles();
            }
        }
    }
}

这篇关于我可以从C ++或C#程序中提取MSI软件包的内容吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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