- 示例源码:windowsservicesample
- servicehelper源码:servicehelper
1. 创建windows service项目,如图:
2. 配置服务参数
3. 安装,启动,停止,卸载服务
实现代码:
private string servicepath => txtservicepath.text.trim(); private string servicename => "servicesample"; private void btnstart_click(object sender, eventargs e) { if (!servicehelper.isexisted(servicename)) { messageboxhelper.showerror($"{servicename}不存在"); return; } servicehelper.start(servicename); } private void btnstop_click(object sender, eventargs e) { if (!servicehelper.isexisted(servicename)) { messageboxhelper.showerror($"{servicename}不存在"); return; } servicehelper.stop(servicename); } private void btninstall_click(object sender, eventargs e) { if (servicehelper.isexisted(servicename)) { messageboxhelper.showerror($"{servicename}已经存在"); return; } servicehelper.install(servicepath); } private void btnuninstall_click(object sender, eventargs e) { if (!servicehelper.isexisted(servicename)) { messageboxhelper.showerror($"{servicename}不存在"); return; } servicehelper.uninstall(servicepath); } }
黄山市民网:https://www.huangshanshimin.com/