以编程方式安装 MSI 包
我想从我的 C# .NET 应用程序以编程方式安装给定的 .msi 包,最好使用我的应用程序指定的安装参数(如安装路径、拒绝垃圾软件等)。
我做了一些搜索,但我还没有真正找到任何有用的东西。最有希望的热门是此主题,但我找不到<的任何文档code>Microsoft.Deployment.WindowsInstaller 或 WindowsInstaller.Installer
的相关内容。
I would like to install a given .msi package programmatically from my C# .NET application, preferably with the installation parameters that my application specifies (like the installation path, decline crapware, etc.).
I did some searches, but I haven't really found anything useful. The most promising hit was this topic, but I cannot find any documentation of Microsoft.Deployment.WindowsInstaller
or of WindowsInstaller.Installer
for that matter.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现上面提到的部署工具基础项目是从 .NET 执行此操作的可靠方法。引用 Microsoft.Deployment.WindowsInstaller.dll 后,使用如下代码安装包:
.NET 包装器的文档位于 Program Files 中 Windows Installer XML 安装目录中的 .chm 文件中。该 DLL 的某些部分松散地包装了本机 Windows API,因此文档 here 也很有用,这就是我如何计算出上面代码片段中的字符串以适合我的情况。
I find the Deployment Tools Foundation project mentioned above to be a solid way to do this from .NET. Having referenced Microsoft.Deployment.WindowsInstaller.dll, use code like this to install a package:
The documentation for the .NET wrapper is in a .chm file in the Windows Installer XML installation directory in Program Files. Some parts of that DLL loosely wrap the native Windows APIs so the documentation here can be useful as well, which is how I worked out the string in the above snippet to suit my situation.
有一个 COM 对象为安装程序提供 API:
首先将对 COM 对象“Microsoft Windows Installer 对象库”的引用添加到您的项目中。然后您可以从以下代码开始:
并且有一个关于 安装程序对象。
There's a COM object that offers an API for the installer:
First add a reference to COM object "Microsoft Windows Installer Object Library" to your project. Then you can start with the following code:
And there's a documentation about the Installer Object.
“部署工具基础”项目是 WIX3.5 安装的一部分,包含大多数(如果不是全部)Windows Installer API 的 .NET 包装器。通过下载并安装 WiX 安装来获取它:http://wixtoolset.org/(当前为 WiX 3.11,8 月更新) .2017)。
在 %ProgramFiles%\Windows Installer XML v3.??\SDK\ 文件夹中找到 Microsoft.Deployment.WindowsInstaller.dll 文件。在您的 C# 项目中设置引用并尝试运行不同的 API,看看是否获得所需的功能。
我强烈建议在 .NET 代码中使用部署工具基础而不是任何 COM 互操作。
The "Deployment Tools Foundation" project which is a part of the WIX3.5 install contains a .NET wrapper for most (if not all) of the Windows Installer API. Get it by downloading and installing the WiX install: http://wixtoolset.org/ (currently WiX 3.11, updated Aug.2017).
Locate the Microsoft.Deployment.WindowsInstaller.dll file in the %ProgramFiles%\Windows Installer XML v3.??\SDK\ folder. Set a reference in your C# project and try to run the different APIs and see if you get the desired functionality.
I highly recommend using Deployment Tools Foundation over any COM Interop from .NET code.
基本的 Win32 API(必要时可以调用)是 MsiInstallProduct。这实际上是所有其他提到的 API 和调用都会结束的地方。
https://msdn.microsoft.com/en- us/library/aa370315(v=vs.85).aspx
只需传递 MSI 文件的完整路径和命令行(包括安静选项等)并检查结果以查看是否它安装正确。
请注意,托管代码有一个简单的 p/invoke 声明:
[DllImport("msi.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern UInt32 MsiInstallProduct(string packagePath, string commandLine);
The basic Win32 API (that can be pinvoked if necessary) is MsiInstallProduct. This is where practically all other mentioned APIs and calls will end up.
https://msdn.microsoft.com/en-us/library/aa370315(v=vs.85).aspx
Just pass the full path to the MSI file and your command line (including quiet options etc) and check the result to see if it installed correctly.
Note that there is a simple p/invoke declaration for managed code:
[DllImport("msi.dll", CharSet = CharSet.Auto, SetLastError=true)]
static extern UInt32 MsiInstallProduct(string packagePath, string commandLine);
最简单的解决方案是使用
msiexec
调用 .msi 上的安装程序。您可以使用命令行设置自定义安装,包括设置 .msi 属性、静默安装等。
The very simplest solution is to use
msiexec
to invoke the installer on the .msi.You can customise the installation using command line settings including setting .msi properties, silent installation etc.
有两种方法可以解决您的问题。
@Glytzhkof 提到的第一个是使用
Microsoft.Deployment.WindowsInstaller .NET 包装器 API
。这是一些非常强大的东西,但需要一些时间来熟悉。您可以在此处获取最新版本(更新:Stein Åsmul 28.12 .2018:DTF 现在是 WiX 工具包的一部分)。另一种方法是使用 转换(.MST 文件)。可以使用 Microsoft Orca 或 InstallShiled 生成转换文件。 MST 包含您需要的所有自定义,并且可以使用此命令行应用到 MSI:
此外,您可以直接在命令行中传递参数:
但是,您需要在
ORCA/InstallShield
来确定实际使用哪些参数。上例中使用的参数不是通用的。
由于存在自定义操作等,实际安装可能会很复杂。事实上,整个行业都是围绕 msi 自定义构建的。它称为
应用程序重新打包
。There are two approaches to solving your problem.
The first one as mentioned by @Glytzhkof is to use the
Microsoft.Deployment.WindowsInstaller .NET wrapper API
. This is some seriously powerful stuff but requires some time to get familiar with. You can get the latest version here (UPDATE: Stein Åsmul 28.12.2018: DTF is now part of the WiX toolkit).The other approach is to use Transforms (.MST files). Transform files can be generated using Microsoft Orca or InstallShiled. The MSTs contains all the customizations that you need and can be applied on the MSI using this command line:
Additionally you can pass parameters directly in the command line:
However, you will need to edit the
MSI
in anORCA/InstallShield
to determine which parameters are actually used.The parameters used in the above example are not universal.
The actual installation can be complicated because of the presence of custom actions etc. In fact there is a whole industry that is built around msi customizations. Its called
Applications Repackaging
.