打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
通过注册表的值,获取软件安装路劲
使用过程中发现问题:用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步可以了。 
这里需要注意的一点是: 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#综合揭秘
3dsMax软件完全卸载方法
解决软件注册表卸载不干净导致的autocad2010无法安装问题。
Wix toolset 安装程序制作总结
Wix 安装部署教程(二)自定义安装界面和行为
C#编程命名规范推荐
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服