• 00
  • :
  • 00
  • :
  • 00

C# windows服务(二) 服务安装与卸载

新建winform程序

 

添加引用:System.Configuration.Install

  

 

界面设计:

  

  代码:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        // 服务 安装
        private void btn_Install_Click(object sender, EventArgs e)
        {

            string serverFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GZ.PlanService.exe");

            var state = new Hashtable();

            AssemblyInstaller installer = new AssemblyInstaller(serverFileName, null);
            installer.UseNewContext = true;
            installer.Install(state);
            installer.Commit(state);


        }
        // 服务卸载
        private void btn_UnInstall_Click(object sender, EventArgs e)
        {
            string serverFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GZ.PlanService.exe");
            var state = new Hashtable();

            AssemblyInstaller installer = new AssemblyInstaller(serverFileName, null);
            installer.UseNewContext = true;
            installer.Uninstall(state);
            installer.Commit(state);
        }


    }

 

  rew

  

posted @ 2020-11-06 11:27  Garson_Zhang  阅读(119)  评论(0编辑  收藏  举报