通過自定義活動(Custom Action)卸載
https://blog.csdn.net/qq_20849387/article/details/78422081
https://blog.csdn.net/qq_20849387/article/details/78422432
https://www.advancedinstaller.com/user-guide/qa-c-sharp-ca.html
https://github.com/wixtoolset/wix3/releases/tag/wix3112rtm
在使用Advanced Installers制作安裝包時,發現卸載功能只會卸載之前打包的文件,程序生成的文件(例如日志文件、緩存信息文件等)不會被刪除,可能導致卸載后再安裝出現問題(ORZ!!好吧,主要是測試和產品要求卸載全部刪除)。
在查資料是發現可以使用Custom Action來實現,例如:https://blog.csdn.net/qq_20849387/article/details/78422081,https://blog.csdn.net/qq_20849387/article/details/78422432
不過網上資料不全,這里做下記錄:
- 安裝WIX TOOLSET,最新版本WiX Toolset v3.11.2,這能保證能夠編譯生成Custom Action。
- 安裝Visual Studio,以2019為例,在拓展里面搜索WIX TOOLSET,安裝WiX Toolset Visual Studio 2019 Extension,這可以讓你在vs添加Custon Action項目
- 以上完成后新建“C# Custom Action Project for WiX v3”項目,然后安裝https://blog.csdn.net/qq_20849387/article/details/78422432的步驟操作即可。
補一段刪除全部文件的代碼
public class CustomActions { [CustomAction] public static ActionResult CustomAction1(Session session) { session.Log("Begin CustomAction1"); string manufacturer = session["Manufacturer"]; string productName = session["ProductName"]; string systembit = Distinguish64or32System(); //通過注冊表的值,獲取軟件安裝路勁 RegistryKey key = Registry.LocalMachine; RegistryKey software; if(systembit == "64") { software = key.OpenSubKey("SOFTWARE\\Wow6432Node\\" + manufacturer + "\\" + productName); } else { software = key.OpenSubKey("SOFTWARE\\" + manufacturer + "\\" + productName); } string installpath = software.GetValue("Path").ToString() + productName; //MessageBox.Show(installpath); DirectoryInfo ip = new DirectoryInfo(installpath); if(ip.Exists) { try { RemoveSubDirectory(ip); ip.Delete(); } catch { return ActionResult.Failure; } } //MessageBox.Show(systembit); return ActionResult.Success; } private static void RemoveSubDirectory(DirectoryInfo uper) { foreach (FileInfo subFile in uper.GetFiles()) { subFile.Delete(); } foreach (DirectoryInfo sub in uper.GetDirectories()) { if (sub.GetFiles().Length > 0 || sub.GetDirectories().Length > 0) RemoveSubDirectory(sub); sub.Delete(); } } private static string Distinguish64or32System() { try { string addressWidth = String.Empty; ConnectionOptions mConnOption = new ConnectionOptions(); ManagementScope mMs = new ManagementScope("//localhost", mConnOption); ObjectQuery mQuery = new ObjectQuery("select AddressWidth from Win32_Processor"); ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(mMs, mQuery); ManagementObjectCollection mObjectCollection = mSearcher.Get(); foreach (ManagementObject mObject in mObjectCollection) { addressWidth = mObject["AddressWidth"].ToString(); } return addressWidth; } catch (Exception ex) { return String.Empty; } } }
在網上查資料時發現以下方案,但是未驗證,后面有時間再填坑。
通過PowerShell卸載
https://blog.csdn.net/WenzhenHe/article/details/92797262
通過ini文件卸載
https://blog.csdn.net/wingWC/article/details/78845152