静默安装
本文关键字:安装 静默 | 更新日期: 2023-09-27 18:06:47
我正在使用C#
编写InstallerClass
,作为安装程序的自定义操作,并且我可以使用InstallerClass
成功运行外部exe(安装(,但当我尝试在InstallerClass
中使用/quiet
时,它不会安装exe。但我可以在命令提示符下使用/quiet
在静默模式下成功安装它。
有什么原因吗?或者如何使用C#在静默模式下安装?
以下是我在Commit方法(overriden(中使用的代码:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = pathExternalInstaller;
p.StartInfo.Arguments = "/quiet";
p.Start();
以下是我用来进行静默安装和卸载的方法:
public static bool RunInstallMSI(string sMSIPath)
{
try
{
Console.WriteLine("Starting to install application");
Process process = new Process();
process.StartInfo.FileName = "msiexec.exe";
process.StartInfo.Arguments = string.Format(" /qb /i '"{0}'" ALLUSERS=1", sMSIPath);
process.Start();
process.WaitForExit();
Console.WriteLine("Application installed successfully!");
return true; //Return True if process ended successfully
}
catch
{
Console.WriteLine("There was a problem installing the application!");
return false; //Return False if process ended unsuccessfully
}
}
public static bool RunUninstallMSI(string guid)
{
try
{
Console.WriteLine("Starting to uninstall application");
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", string.Format("/c start /MIN /wait msiexec.exe /x {0} /quiet", guid));
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process process = Process.Start(startInfo);
process.WaitForExit();
Console.WriteLine("Application uninstalled successfully!");
return true; //Return True if process ended successfully
}
catch
{
Console.WriteLine("There was a problem uninstalling the application!");
return false; //Return False if process ended unsuccessfully
}
}
这对我有用。
Process process = new Process();
process.StartInfo.FileName = @ "C:'PATH'Setup.exe";
process.StartInfo.Arguments = "/quiet";
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.Start();
process.WaitForExit();
您是否尝试使用安装参数中列出的/Q
或/QB
参数?它可能看起来像这样:
p.StartInfo.Arguments = "/Q";
我从这份文件中了解到:http://msdn.microsoft.com/en-us/library/ms144259(v=sql.100(.aspx
以下是我为所有用户静默安装应用程序的逻辑:
public void Install(string filePath)
{
try
{
Process process = new Process();
{
process.StartInfo.FileName = filePath;
process.StartInfo.Arguments = " /qb ALLUSERS=1";
process.EnableRaisingEvents = true;
process.Exited += process_Exited;
process.Start();
process.WaitForExit();
}
}
catch (InvalidOperationException iex)
{
Interaction.MsgBox(iex.Message, MsgBoxStyle.OkOnly, MethodBase.GetCurrentMethod().Name);
}
catch (Exception ex)
{
Interaction.MsgBox(ex.Message, MsgBoxStyle.OkOnly, MethodBase.GetCurrentMethod().Name);
}
}
private void process_Exited(object sender, EventArgs e)
{
var myProcess = (Process)sender;
if (myProcess.ExitCode == 0)
// do yours here...
}
string filePath = @"C:'Temp'Something.msi";
Process.Start(filePath, @"/quiet").WaitForExit();
它对我有效。