忽略TestHarness.Logger.Log, 这是自定义的写日志的方法, 可以替换成Console.WriteLine()
public static int InvokeMsi(string msiFilePath, string workDir, bool wait, bool uninstall)
{
Process process = null;
string arg = String.Format("/q /{0} \"{1}\"", uninstall ? "x" : "i", msiFilePath);
ProcessStartInfo psi = new ProcessStartInfo("msiexec.exe", arg);
psi.UseShellExecute = false;
if (!String.IsNullOrEmpty(workDir))
{
psi.WorkingDirectory = workDir;
}
else
{
psi.WorkingDirectory = Environment.CurrentDirectory;
}
process = new Process();
process.StartInfo = psi;
try
{
TestHarness.Logger.Log("Starting to {0} using msiexec.exe" + arg, (uninstall ? "uninstall" : "install") + msiFilePath);
process.Start();
}
catch (Exception ex)
{
TestHarness.Logger.Log("Exception: " + ex.Message);
return -1;
}
if (wait)
{
process.WaitForExit();
if (process.ExitCode == 0)
{
TestHarness.Logger.Log("Succeeded! ");
}
return process.ExitCode;
}
else
{
return 0;
}
}
//Install software by using msiexec
public static int InstallMsi(string msiFilePath, string workDir, bool wait)
{
return InvokeMsi(msiFilePath, workDir, wait, false);
}
public static int UninstallMsi(string msiFilePath, string workDir, bool wait)
{
return InvokeMsi(msiFilePath, workDir, wait, true);
}