2 using System.Diagnostics;
\r
3 using System.Text.RegularExpressions;
\r
4 using System.Collections;
\r
5 using System.CodeDom.Compiler;
\r
8 namespace NaGet.Packages.Install
\r
11 /// Description of Uninstallation.
\r
13 public class Uninstallation
\r
18 public InstalledPackage UninstalledPackage;
\r
21 /// 外部アプリのエラー出力の受信ハンドラ
\r
23 public event EventHandler<NaGet.Utils.AnyDataEventArgs<string>> ErrorDataReceived;
\r
26 /// 外部アプリの標準出力の受信ハンドラ
\r
28 public event EventHandler<NaGet.Utils.AnyDataEventArgs<string>> OutputDataReceived;
\r
31 /// サイレントアンインストールするか否か
\r
33 public bool Silent = false;
\r
38 /// <param name="package">アンインストールするパッケージ</param>
\r
39 public Uninstallation(InstalledPackage package)
\r
41 UninstalledPackage = package;
\r
47 public bool Installed
\r
50 if (UninstalledPackage.Type == InstallerType.ARCHIVE) {
\r
51 return Directory.Exists(UninstalledPackage.UninstallInfo.InstallLocation);
\r
53 foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {
\r
54 if (! string.IsNullOrEmpty(UninstalledPackage.UninstallerKey)) {
\r
55 Match match = Regex.Match(info.DisplayName, UninstalledPackage.UninstallerKey);
\r
57 if (match.Success) {
\r
68 /// アンインストーラ等を起動してアンインストール作業を行う
\r
70 /// <returns>アンインストーラの終了コード</returns>
\r
71 public int Uninstall()
\r
74 throw new ApplicationException("Program not found, may be already uninstalled");
\r
78 string uninstallString = Silent? UninstalledPackage.UninstallInfo.QuietUninstallString : UninstalledPackage.UninstallInfo.UninstallString;
\r
79 if (string.IsNullOrEmpty(uninstallString)) {
\r
80 throw new ApplicationException(string.Format("Could not found {0}install script", Silent? "silent " : ""));
\r
83 if (UninstalledPackage.UninstallInfo.WindowsInstaller &&
\r
84 Regex.Match(uninstallString.Substring("MsiExec.exe /I".Length),
\r
85 @"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").Success) {
\r
86 string guid = uninstallString.Substring("MsiExec.exe /I".Length);
\r
87 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(
\r
88 new ProcessStartInfo("msiexec", string.Format("/X{0}", guid)),
\r
89 NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),
\r
90 NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {
\r
92 if (NaGet.Env.InstallProcessOnBackground) {
\r
94 hProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
95 } catch (Exception) {}
\r
98 hProcess.WaitForExit();
\r
100 exitValue = hProcess.ExitCode;
\r
102 } else if (File.Exists(uninstallString)) {
\r
104 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(
\r
105 new ProcessStartInfo(uninstallString),
\r
106 NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),
\r
107 NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {
\r
109 if (NaGet.Env.InstallProcessOnBackground) {
\r
111 hProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
112 } catch (Exception) {}
\r
115 hProcess.WaitForExit();
\r
117 exitValue = hProcess.ExitCode;
\r
120 ProcessStartInfo procInfo = new ProcessStartInfo(null, uninstallString);
\r
121 procInfo.UseShellExecute = false;
\r
122 if (UninstalledPackage.Type == InstallerType.ARCHIVE) {
\r
123 procInfo.CreateNoWindow = true;
\r
125 using (NaGet.InteropServices.CreateProcessCaller p = new NaGet.InteropServices.CreateProcessCaller(procInfo)) {
\r
127 if (NaGet.Env.InstallProcessOnBackground) {
\r
129 p.PriorityClass = ProcessPriorityClass.Idle;
\r
130 } catch (Exception) {}
\r
135 exitValue = p.ExitCode;
\r
142 public override string ToString()
\r
144 return string.Format("{0}({1})", UninstalledPackage.Name, UninstalledPackage.Version);
\r