14

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.

7
  • @David Heffernan: I guess, but would it do what I want it to do?
    – ShdNx
    Commented Apr 23, 2011 at 16:17
  • Well, msiexec is the most common way to invoke installer Commented Apr 23, 2011 at 16:19
  • @David Heffernan: I checked out the documentation, and it does appear to allow non-gui installation, however, I cannot see anything that would allow me configure the setup (e.g. set installation path). Any clues to that?
    – ShdNx
    Commented Apr 23, 2011 at 17:13
  • That all depends on the .msi. You'd need to pass settings specific to the particular .msi Commented Apr 23, 2011 at 17:23
  • @David Heffernan: how can I do that? Could you please elaborate your idea in an answer, so that I can accept if it proves to be the best solution?
    – ShdNx
    Commented Apr 23, 2011 at 17:58

6 Answers 6

14

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:

Installer.SetInternalUI(InstallUIOptions.Silent);
Installer.InstallProduct(msiFilename, "ACTION=INSTALL ALLUSERS=2 MSIINSTALLPERUSER=");

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.

1
  • How to check whether MSI installation is completed or not? Please guide me to achieve this.
    – cheran
    Commented Jul 27, 2018 at 10:40
8

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:

using System;
using WindowsInstaller;

namespace TestApp
{
    public class InstallerTest
    {
        public static void Install()
        {
            Type type = Type.GetTypeFromProgID("WindowsInstaller.Installer");
            Installer installer = (Installer)Activator.CreateInstance(type);
            installer.InstallProduct("YourPackage.msi");
        }
    }
}

And there's a documentation about the Installer Object.

2
  • According to the documentation, the InstallProduct will display the GUI wizard to the user. My aim would be to completely automate the installation. Do you have an idea for that?
    – ShdNx
    Commented Apr 23, 2011 at 16:18
  • Have you tried "installer.UILevel = MsiUILevel.msiUILevelNone;"? I've never used it. But it's worth a try.
    – Codo
    Commented Apr 23, 2011 at 16:34
7

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.

2
  • Thanks, I'll give it a try, but I was hoping that there was some online documentation that could have given me some hints...
    – ShdNx
    Commented Apr 26, 2011 at 20:35
  • Check the help file at %ProgramFiles%\Windows Installer XML v3.5\doc\DTF.chm. Also check out: wix.tramontana.co.hu/tutorial Commented Feb 18, 2014 at 19:11
4

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);

3

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.

1
  • 1
    How can I detect completion of an msi installer? Is Process.WaitForExit sufficient? Commented Feb 26, 2020 at 8:30
3

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:

msiexec /i somemsi.msi TRANSFORMS=somemst.mst /qb

Additionally you can pass parameters directly in the command line:

msiexec /i <somemsi.msi> /qb AGREETOLICENSE=YES INSTALLDIR=C:\Temp
etc...

However, you will need to edit the MSI in an ORCA/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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.