代碼觸發安裝 靜默執行
對於一些不關注安裝過程的情況來說,比如軟件先通過下載器下載,下載完直接觸發安裝
private void StartProcess(string filePath, string parameters, Action<string> callback) { try { Process myprocess = new Process(); ProcessStartInfo startInfo; if (!string.IsNullOrEmpty(parameters)) { startInfo = new ProcessStartInfo(filePath, parameters); } else { startInfo = new ProcessStartInfo(filePath); } myprocess.StartInfo = startInfo; myprocess.StartInfo.Verb = "runas"; myprocess.Start(); myprocess.WaitForExit(); callback("程序安裝成功!"); } catch (Exception ex) { callback("程序安裝失敗,請聯繫管理員!"); //LogUtil.WriteLog(ex.Message); } }
myprocess.StartInfo = startInfo;
myprocess.StartInfo.Verb = "runas";
myprocess.Start();
myprocess.WaitForExit();
myprocess.WaitForExit();不能忽略,忽略會中斷安裝運行
string parameter = string.Format(@"/i {0} /quiet /qn /norestart /log c:\install.log", filename); StartProcess(@"msiexec.exe", parameter, tip => { installTip.Text = tip; });
參數中filename是msi文件的安裝路徑