可以通过cmd命令行来执行,也可以通过代码来执行,一般都需要管理员权限运行
代码
/// <summary>
/// 静默安装
/// </summary>
/// <param name="fileName">安装文件路径</param>
/// <param name="arguments">安装参数</param>
/// <param name="isRunas">是否以管理员权限运行</param>
/// <returns></returns>
public static void SilentInstall(string fileName, string arguments, bool isRunas = true, bool isCreateNoWindow = false)
{
Process processSetup = new Process();
processSetup.StartInfo.FileName = fileName;
if (isCreateNoWindow)
processSetup.StartInfo.CreateNoWindow = true;
processSetup.StartInfo.Arguments = arguments;
processSetup.EnableRaisingEvents = true;
if (isRunas)
processSetup.StartInfo.Verb = "runas";
processSetup.Exited += delegate { };
processSetup.Start();
processSetup.WaitForExit();
var exitCode = processSetup.ExitCode;
processSetup.Close();
}
例子
//软件安装
SilentInstall(@"C:\Test.msi", @" /quiet /n");
//SQL SERVER安装
string saPwd = "123";
string arguments = $@"/q /ACTION=Install /FEATURES=""SQLENGINE, REPLICATION, SNAC_SDK"" /INSTANCENAME=""SQLExpress"" /SECURITYMODE=""SQL"" /SAPWD=""{saPwd}"" /IACCEPTSQLSERVERLICENSETERMS /UpdateEnabled=False";
SilentInstall(file, arguments);
卸载程序
SilentInstall("msiexec.exe", $" /quiet /n /uninstall {testProductCode}");
ProductCode为安装程序的ProductCode属性值
参考
静默安装_weixin_30741653的博客-CSDN博客
C#卸载软件 msiexec安装参数详解 - 爱码网
Installshield之静默安装_setup.iss_Blue_sky90的博客-CSDN博客
SQLserver静默安装(命令行安装)_Dan淡淡的心的博客-CSDN博客