【问题标题】:C# Silent installation of msi does not workC#静默安装msi不起作用
【发布时间】:2020-06-17 09:27:13
【问题描述】:

我想在 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) 中包含创建日志文件,该文件也会创建,但为空。

有人可以帮我吗?

非常感谢!!!

【问题讨论】:

    标签: c# installation windows-installer silent-installer


    【解决方案1】:

    您也应该使用提升的权限执行新进程:

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

    【讨论】:

    • 瓦迪姆,你是明星,谢谢。你有什么想法吗,如果还有可能 1) 等到安装过程完成 2) 获得安装过程的结果或状态
    • 进程启动后,你应该得到新的Process-object。 Process.Start 返回新创建的进程 -> Process proc = Process.Start(startInfo);。 1)等到安装过程完成:proc.WaitForExit(); 2)获取过程结果:proc.ExitCode(0 - 成功,所有其他值 - 失败)
    猜你喜欢
    • 1970-01-01
    • 2023-04-06
    • 2013-03-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多