using System; using System.Diagnostics; using System.Text.RegularExpressions; using System.Collections; using System.CodeDom.Compiler; using System.IO; namespace NaGet.Packages.Install { /// /// Description of Uninstallation. /// public class Uninstallation { /// /// アンインストールするパッケージ /// public InstalledPackage UninstalledPackage; /// /// 外部アプリのエラー出力の受信ハンドラ /// public event EventHandler> ErrorDataReceived; /// /// 外部アプリの標準出力の受信ハンドラ /// public event EventHandler> OutputDataReceived; /// /// サイレントアンインストールするか否か /// public bool Silent = false; /// /// コンストラクタ /// /// アンインストールするパッケージ public Uninstallation(InstalledPackage package) { UninstalledPackage = package; } /// /// インストールされた状態か否か /// public bool Installed { get { if (Directory.Exists(UninstalledPackage.UninstallInfo.InstallLocation)) { return true; } else if (UninstalledPackage.Type == InstallerType.ARCHIVE) { return false; } foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) { Match match = Regex.Match(info.DisplayName, UninstalledPackage.UninstallerKey); if (match.Success) { return true; } } return false; } } /// /// アンインストーラ等を起動してアンインストール作業を行う /// /// アンインストーラの終了コード public int Uninstall() { if (! Installed) { throw new ApplicationException("Program not found, may be already uninstalled"); } int exitValue = 0; string uninstallString = Silent? UninstalledPackage.UninstallInfo.QuietUninstallString : UninstalledPackage.UninstallInfo.UninstallString; if (string.IsNullOrEmpty(uninstallString)) { throw new ApplicationException(string.Format("Could not found {0}install script", Silent? "silent " : "")); } if (UninstalledPackage.UninstallInfo.WindowsInstaller && Regex.Match(uninstallString.Substring("MsiExec.exe /I".Length), @"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").Success) { string guid = uninstallString.Substring("MsiExec.exe /I".Length); using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture( new ProcessStartInfo("msiexec", string.Format("/X{0}", guid)), NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived), NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) { hProcess.WaitForExit(); exitValue = hProcess.ExitCode; } } else if (File.Exists(uninstallString)) { // 単独のファイルの場合 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture( new ProcessStartInfo(uninstallString), NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived), NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) { hProcess.WaitForExit(); exitValue = hProcess.ExitCode; } } else { ProcessStartInfo procInfo = new ProcessStartInfo(null, uninstallString); procInfo.UseShellExecute = false; if (UninstalledPackage.Type == InstallerType.ARCHIVE) { procInfo.CreateNoWindow = true; } using (NaGet.InteropServices.CreateProcessCaller p = new NaGet.InteropServices.CreateProcessCaller(procInfo)) { p.WaitForExit(); exitValue = p.ExitCode; } } return exitValue; } public override string ToString() { return string.Format("{0}({1})", UninstalledPackage.Name, UninstalledPackage.Version); } } }