Search code examples
c#installationwindows-installersilent-installer

C# Silent installation of msi does not work


i want to create a silent installation of a msi in c#. I already found the right command within the command line: msiexec /i c:\temp\Setup1.msi /quiet /qn /norestart /log c:\temp\install.log ALLUSERS=1. When i run this command in command line with admin rights, everything works fine.

I now want to do the same in c#. I already implemented an app.manifest file (so that the user only can open the program with admin rights): <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />.

I searched the internet for days and tried so many other stuff - nothing worked.

Here are some tries:

System.Diagnostics.Process installerProcess;
installerProcess = System.Diagnostics.Process.Start("cmd.exe", @"msiexec /i C:\temp\Setup1.msi /quiet /qn /norestart ALLUSERS=1");

while (installerProcess.HasExited == false)
{
    System.Threading.Thread.Sleep(250);
}

or

System.Diagnostics.Process installerProcess;
installerProcess = System.Diagnostics.Process.Start(@"C:\temp\Setup1.msi", "/quiet /qn /norestart ALLUSERS=1");

while (installerProcess.HasExited == false)
{
    System.Threading.Thread.Sleep(250);
}

In my desperation, i also created a batch just with the line which works in cmd, and tries to execute this batch in c#, but i also failed:

File.WriteAllText(@"C:\temp\Setup1.bat", @"msiexec /i c:\temp\Setup1.msi /quiet /qn /norestart ALLUSERS=1");

ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = @"C:\temp\Setup1.bat";
si.UseShellExecute = false;
System.Diagnostics.Process.Start(si);

Nothing works. The program code is run through without errors, nothing is installed. Even if I include a log file creation in the arguments (/log c:\temp\install.log) this file is created, but is empty.

Can someone help me here?

Thank you so much!!!


Solution

  • You should execute new process with elevated rights as well:

      string msiPath = @"C:\temp\Setup1.msi";
      string winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
      ProcessStartInfo startInfo = new ProcessStartInfo(Path.Combine(winDir, @"System32\msiexec.exe"), $"/i {msiPath} /quiet /qn /norestart ALLUSERS=1");
      startInfo.Verb = "runas";
      startInfo.UseShellExecute = true;
      Process.Start(startInfo);