Advanced Installer卸载多余文件,用户自定义动作,Custom Action

通过自定义活动(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/78422081https://blog.csdn.net/qq_20849387/article/details/78422432

不过网上资料不全,这里做下记录:

  1. 安装WIX TOOLSET,最新版本WiX Toolset v3.11.2,这能保证能够编译生成Custom Action。
  2. 安装Visual Studio,以2019为例,在拓展里面搜索WIX TOOLSET,安装WiX Toolset Visual Studio 2019 Extension,这可以让你在vs添加Custon Action项目
  3. 以上完成后新建“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

原文地址:https://www.cnblogs.com/MarcLiu/p/13608518.html