Reputation: 117
I'm trying to quietly install a .msi file using C# without the need for any user input. I'm having trouble trying to get past the license agreement so that the installation can continue. Is there a way to pass an argument so that the agreement is accepted without any user imput?
Upvotes: 2
Views: 24870
Reputation: 42186
Caution: I would avoid triggering software installation from within an application binary - unless you are making an actual setup launcher application. It can work, but it may trigger serious problems with anti-virus and malware scanners. I have seen that before. I would assume you would also need to run elevated - with admin rights - to kick off your installs (per-machine installations).
Some digressions and suggestions first: MSI can be installed via msiexec.exe commands
, Powershell
, DTF C#
(see below), WMI
, MSI API
(COM
, Win32
).
Batch: With that said, why don't you just install using a regular batch file? The /QN switch will bypass the entire setup GUI-sequence and then there should be no need to accept any license agreements. MSI logging information (short version: open log and search for "value 3" to find errors).
This command must be run from an elevated command prompt (admin rights):
msiexec.exe /I "Installer.msi" /QN /L* "C:\msilog.log" ALLUSERS=1
Setup.exe: You can also make a WiX Burn bundle (see link for code mockup) or use some sort of other tool to make a setup.exe
that will install your original application and then other components in sequence - so there is nothing to trigger to install from the application.
Interactive Install: If you want to install with some user interaction you can locate the property controlling the accept status of the license agreement and set that to the appropriate value - usually 1 - to indicate accepted license.
DTF: Now the code answer. There is a component installed with the WiX toolkit called DTF - Desktop Tools Foundation
It has a number of C# classes designed to deal with MSI files via managed code. This answer explains what file to add as a reference in Visual Studio and describes the different files / assemblies of DTF briefly. Another DTF sample.
The relevant file for your described purpose is: "Microsoft.Deployment.WindowsInstaller.dll"
.
NOTE!
You must launch Visual Studio with admin rights to install per machine
."%ProgramFiles(x86)%\WiX Toolset v3.11\bin\Microsoft.Deployment.WindowsInstaller.dll"
.The below code tested in VS2017 (a more elaborate version with admin check here - zipped):
using System;
using Microsoft.Deployment.WindowsInstaller;
// RUN WITH ADMIN RIGHTS
namespace DTFTest
{
class Program
{
static void Main(string[] args)
{
try
{
Installer.SetInternalUI(InstallUIOptions.Silent);
Installer.EnableLog(InstallLogModes.Verbose, @"E:\Install.log");
Installer.InstallProduct(@"E:\Install.msi", "");
}
catch (Exception ex)
{
Console.WriteLine("Exception:\n\n " + ex.Message);
}
}
}
}
Link:
Upvotes: 3
Reputation: 55601
Generally you just run the msi silently like
msiexec /i foo.msi /qn
If the MSI has implemented an additional consent you might have to pass a property like
msiexec /i foo.msi /qn ACCEPTEULA=1
The exact name of this property would depend on the MSI so you'd need to consult vendor documentation or examine the MSI using a tool such as ORCA.
Upvotes: 1