AdvancedInstaller用户自定义动作——卸载程序

来源:互联网 发布:数据库文件类型 编辑:程序博客网 时间:2024/05/29 14:31

使用过程中发现问题:用AdvancedInstaller本身自带的卸载程序会出现卸载不干净的问题,有残留日志文件,注册文件等。。。
通过实验得知可以通过用户自定义动作(Custom Actions)可以强制删除所有文件。如何建立自定义动作请看我这篇博客:http://blog.csdn.net/qq_20849387/article/details/78421482
今天直接给出一个用于删除程序安装路径下的所有文件的用户自定义动作代码:

 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;            }        }    }

拿着这段代码制作用户自定义的动作。会生成一个CustomAction1.CA.dll文件,这个文件就是我们需要的用户自定义文件,然后参照这篇博客:http://blog.csdn.net/qq_20849387/article/details/78422081
第11步可以了。
这里需要注意的一点是:
这里写图片描述

阅读全文
0 0
原创粉丝点击