今天终于搞定了安装项目中的自定义操作,庆祝一下!

在安装项目中可以执行自定义操作,包括制定一个exe dll vbs js等类型的文件。之前我们是利用一个exe,但感觉使用起来限制很多,如不方便定制,一次只执行一个exe,不方便传参数等。在.net中可以写出一个安装类来,通过override出Commit、install 、Uninstall等函数实现自定义操作。这里是一个实际的示例:


namespace InstHelper
{
    [RunInstaller(true)]
    public partial class InstHelper : Installer
    {
        public InstHelper()
        {
            InitializeComponent();
        }

        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);
            string exepath;
            //注册服务
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(" create DaykeyPcManager binpath= \"{0}\\PcManagerSvc.exe\"", path);
            
            System.Diagnostics.Process.Start("sc.exe", sb.ToString());  
        
            //配置服务
            sb.Length = 0;
            sb.Append("config DaykeyPcManager type= interact type= own");
            System.Diagnostics.Process.Start("sc.exe", sb.ToString());

            //添加防火墙列外
            sb.Length = 0;
            sb.AppendFormat(" firewall add allowedprogram \"{0}\\PcManagerSvc.exe\" ABCD ENABLE", path);
            System.Diagnostics.Process.Start("netsh.exe", sb.ToString());

            //启动服务
            exepath = path + "\\start.bat";
            System.Diagnostics.Process.Start(exepath);
        }

        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            base.Uninstall(savedState);

            //停止服务
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string exepath = path + "\\stop.bat";
            System.Diagnostics.Process.Start(exepath);

            //删除防火墙列外
            StringBuilder sb = new StringBuilder();
            sb.Length = 0;
            sb.AppendFormat(" firewall delete allowedprogram \"{0}\\PcManagerSvc.exe\"", path);
            System.Diagnostics.Process.Start("netsh.exe", sb.ToString());

            //删除服务
            sb.Length = 0;
            sb.Append("delete DaykeyPcManager");
            System.Diagnostics.Process.Start("sc.exe", sb.ToString());
        }

        protected override void OnBeforeUninstall(System.Collections.IDictionary savedState)
        {
            base.OnBeforeUninstall(savedState);
            //停止服务
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string exepath = path + "\\stop.bat";
            System.Diagnostics.Process.Start(exepath);
        }
        //public override void Install(System.Collections.IDictionary stateSaver)
        //{
        //    base.Install(stateSaver);
        //    System.Diagnostics.Process.Start("http://www.csdn.net");
        //}
    }
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值