2

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!!!

nicetomitja
  • 145
  • 10

1 Answers1

0

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);
Vadim
  • 471
  • 6
  • 15
  • Vadim, you're a star, thank you. Do you have an idea, if there is also a possibility to 1) wait until instalaltion process is finished 2) get result or state of installation process – nicetomitja Jun 18 '20 at 05:13
  • After the process was started, you should get the new ```Process```-object. ```Process.Start``` returns newly created process -> ```Process proc = Process.Start(startInfo);```. 1) to wait until installation process is finished: ```proc.WaitForExit();``` 2) get result of the process: ```proc.ExitCode``` (0 - success, all other values - failed) – Vadim Jun 18 '20 at 06:04