C# 无声安装MSI失败

3
我想在C#中创建一个MSI的静默安装。我已经在命令行中找到了正确的命令: msiexec /i c:\temp\Setup1.msi /quiet /qn /norestart /log c:\temp\install.log ALLUSERS=1。当我用管理员权限在命令行中运行这个命令时,一切都正常。
现在我想在C#中做同样的事情。我已经实现了一个app.manifest文件(这样用户只能以管理员权限打开程序): <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
我在网上搜索了几天,尝试了很多其他方法 - 没有任何效果。
以下是一些尝试:
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);
}

或者

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

在我绝望的时候,我创建了一个批处理文件,其中只包含在cmd中有效的行,并尝试在c#中执行此批处理文件,但我也失败了。
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);

什么都不起作用。程序代码运行没有错误,没有安装任何东西。即使我在参数中包含一个日志文件创建(/log c:\temp\install.log),这个文件也会被创建,但是它是空的。

有人可以帮助我吗?

非常感谢!!!

1个回答

2

你应该以提升的权限执行新的进程:

  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,你真是个天才,谢谢你。你有没有想法,是否还有可能: 1)等待安装过程完成 2)获取安装过程的结果或状态 - undefined
1
在启动过程之后,您应该获得新的Process对象。Process.Start返回新创建的进程 -> Process proc = Process.Start(startInfo);。1) 等待安装过程完成:proc.WaitForExit(); 2) 获取进程的结果:proc.ExitCode(0 - 成功,其他所有值 - 失败) - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接