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
慎于行,敏于思!GGGGGG