评论

收藏

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

编程语言 编程语言 发布于:2021-07-27 15:05 | 阅读数:443 | 评论:0

新建winform程序
添加引用:System.Configuration.Install
DSC0000.png

界面设计:
DSC0001.png
代码:
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
慎于行,敏于思!GGGGGG