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 || UninstalledPackage.Type == InstallerType.ITSELF) {
\r
52 return Directory.Exists(UninstalledPackage.UninstallInfo.InstallLocation);
\r
54 foreach (UninstallInformation info in RegistriedUninstallers.Uninstallers) {
\r
55 if (! string.IsNullOrEmpty(UninstalledPackage.UninstallerKey)) {
\r
56 Match match = Regex.Match(info.DisplayName, UninstalledPackage.UninstallerKey);
\r
58 if (match.Success) {
\r
69 /// アンインストーラ等を起動してアンインストール作業を行う
\r
71 /// <returns>アンインストーラの終了コード</returns>
\r
72 public int Uninstall()
\r
75 throw new ApplicationException("Program not found, may be already uninstalled");
\r
79 string uninstallString = Silent? UninstalledPackage.UninstallInfo.QuietUninstallString : UninstalledPackage.UninstallInfo.UninstallString;
\r
80 if (string.IsNullOrEmpty(uninstallString)) {
\r
81 throw new ApplicationException(string.Format("Could not found {0}install script", Silent? "silent " : ""));
\r
84 if (UninstalledPackage.UninstallInfo.WindowsInstaller &&
\r
85 Regex.Match(uninstallString.Substring("MsiExec.exe /I".Length),
\r
86 @"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").Success) {
\r
87 string guid = uninstallString.Substring("MsiExec.exe /I".Length);
\r
88 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(
\r
89 new ProcessStartInfo("msiexec", string.Format("/X{0}", guid)),
\r
90 NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),
\r
91 NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {
\r
93 if (NaGet.Env.InstallProcessOnBackground) {
\r
95 hProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
96 } catch (Exception) {}
\r
99 hProcess.WaitForExit();
\r
101 exitValue = hProcess.ExitCode;
\r
103 } else if (File.Exists(uninstallString)) {
\r
105 using (Process hProcess = NaGet.Utils.ProcessStartWithOutputCapture(
\r
106 new ProcessStartInfo(uninstallString),
\r
107 NaGet.Utils.ConvertToDataReceivedEventHandler(OutputDataReceived),
\r
108 NaGet.Utils.ConvertToDataReceivedEventHandler(ErrorDataReceived)) ) {
\r
110 if (NaGet.Env.InstallProcessOnBackground) {
\r
112 hProcess.PriorityClass = ProcessPriorityClass.Idle;
\r
113 } catch (Exception) {}
\r
116 hProcess.WaitForExit();
\r
118 exitValue = hProcess.ExitCode;
\r
121 ProcessStartInfo procInfo = new ProcessStartInfo(null, uninstallString);
\r
122 procInfo.UseShellExecute = false;
\r
123 procInfo.CreateNoWindow = true;
\r
124 using (NaGet.InteropServices.CreateProcessCaller p = new NaGet.InteropServices.CreateProcessCaller(procInfo)) {
\r
126 if (NaGet.Env.InstallProcessOnBackground) {
\r
128 p.PriorityClass = ProcessPriorityClass.Idle;
\r
129 } catch (Exception) {}
\r
134 exitValue = p.ExitCode;
\r
141 public override string ToString()
\r
143 return string.Format("{0}({1})", UninstalledPackage.Name, UninstalledPackage.Version);
\r