C#版Windows服务安装卸载小工具
程序员文章站
2023-08-29 20:59:48
前言
在我们的工作中,经常遇到windows服务的安装和卸载,在之前公司也普写过一个winform程序选择安装路径,这次再来个小巧灵活的控制台程序,不用再选择...
前言
在我们的工作中,经常遇到windows服务的安装和卸载,在之前公司也普写过一个winform程序选择安装路径,这次再来个小巧灵活的控制台程序,不用再选择,只需放到需要安装服务的目录中运行就可以实现安装或卸载。
开发思路
1、由于系统的权限限制,在运行程序时需要以管理员身份运行
2、因为需要实现安装和卸载两个功能,在程序运行时提示本次操作是安装还是卸载 需要输入 1 或 2
3、接下来程序会查找当前目录中的可执行文件并过滤程序本身和有时我们复制进来的带有vhost的文件,并列出列表让操作者选择(一般情况下只有一个)
4、根据用户所选进行安装或卸载操作
5、由于可能重复操作,需要递归调用一下
具体实现
首先们要操作服务,需要用 system.serviceprocess 来封装实现类
using system; using system.collections; using system.configuration.install; using system.linq; using system.serviceprocess; namespace autoinstallutil { public class systemservices { /// <summary> /// 打开系统服务 /// </summary> /// <param name="servicename">系统服务名称</param> /// <returns></returns> public static bool systemserviceopen(string servicename) { try { using (var control = new servicecontroller(servicename)) { if (control.status != servicecontrollerstatus.running) { control.start(); } } return true; } catch { return false; } } /// <summary> /// 关闭系统服务 /// </summary> /// <param name="servicename">系统服务名称</param> /// <returns></returns> public static bool systemserviceclose(string servicename) { try { using (var control = new servicecontroller(servicename)) { if (control.status == servicecontrollerstatus.running) { control.stop(); } } return true; } catch { return false; } } /// <summary> /// 重启系统服务 /// </summary> /// <param name="servicename">系统服务名称</param> /// <returns></returns> public static bool systemservicerestart(string servicename) { try { using (var control = new servicecontroller(servicename)) { if (control.status == system.serviceprocess.servicecontrollerstatus.running) { control.continue(); } } return true; } catch { return false; } } /// <summary> /// 返回服务状态 /// </summary> /// <param name="servicename">系统服务名称</param> /// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns> public static int getsystemservicestatus(string servicename) { try { using (var control = new servicecontroller(servicename)) { return (int)control.status; } } catch { return 0; } } /// <summary> /// 返回服务状态 /// </summary> /// <param name="servicename">系统服务名称</param> /// <returns>1:服务未运行 2:服务正在启动 3:服务正在停止 4:服务正在运行 5:服务即将继续 6:服务即将暂停 7:服务已暂停 0:未知状态</returns> public static string getsystemservicestatusstring(string servicename) { try { using (var control = new servicecontroller(servicename)) { var status = string.empty; switch ((int)control.status) { case 1: status = "服务未运行"; break; case 2: status = "服务正在启动"; break; case 3: status = "服务正在停止"; break; case 4: status = "服务正在运行"; break; case 5: status = "服务即将继续"; break; case 6: status = "服务即将暂停"; break; case 7: status = "服务已暂停"; break; case 0: status = "未知状态"; break; } return status; } } catch { return "未知状态"; } } /// <summary> /// 安装服务 /// </summary> /// <param name="statesaver"></param> /// <param name="filepath"></param> public static void installservice(idictionary statesaver, string filepath) { try { var myassemblyinstaller = new assemblyinstaller { usenewcontext = true, path = filepath }; myassemblyinstaller.install(statesaver); myassemblyinstaller.commit(statesaver); myassemblyinstaller.dispose(); } catch (exception ex) { throw new exception("installserviceerror/n" + ex.message); } } public static bool serviceisexisted(string servicename) { servicecontroller[] services = servicecontroller.getservices(); return services.any(s => s.servicename == servicename); } /// <summary> /// 卸载服务 /// </summary> /// <param name="filepath">路径和文件名</param> public static void uninstallservice(string filepath) { try { //uninstall service var myassemblyinstaller = new assemblyinstaller { usenewcontext = true, path = filepath }; myassemblyinstaller.uninstall(null); myassemblyinstaller.dispose(); } catch (exception ex) { throw new exception("uninstallserviceerror/n" + ex.message); } } } }
接下来我们封装控制台的操作方法为了实现循环监听这里用了递归
using system; using system.diagnostics; using system.io; using system.linq; namespace autoinstallutil { class program { static void main(string[] args) { try { serveraction(); } catch (exception ex) { console.writeline("发生错误:{0}", ex.message); } console.readkey(); } /// <summary> /// 操作 /// </summary> private static void serveraction() { console.writeline("请输入:1安装 2卸载"); var condition = console.readline(); var currentpath = environment.currentdirectory; var currentfilenamevshost = path.getfilename(process.getcurrentprocess().mainmodule.filename).tolower(); var currentfilename = currentfilenamevshost.replace(".vshost.exe", ".exe"); var files = directory.getfiles(currentpath) .select(o => path.getfilename(o).tolower()) .tolist() .where( o => o != currentfilenamevshost && o != currentfilename && o.tolower().endswith(".exe") && o != "installutil.exe" && !o.tolower().endswith(".vshost.exe")) .tolist(); if (files.count == 0) { console.writeline("未找到可执行文件,请确认当前目录有需要安装的服务程序"); } else { console.writeline("找到目录有如下可执行文件,请选择需要安装或卸载的文件序号:"); } int i = 0; foreach (var file in files) { console.writeline("序号:{0} 文件名:{1}", i, file); i++; } var servicefileindex = console.readline(); var servicepathname = currentpath + "\\" + files[convert.toint32(servicefileindex)]; if (condition == "1") { systemservices.installservice(null, servicepathname); } else { systemservices.uninstallservice(servicepathname); } console.writeline("**********本次操作完毕**********"); serveraction(); } } }
到此为止简单的安装程序就写完了,为了醒目我选了个红色的西红柿来做为图标,这样显示些
源码和程序:安装卸载windows服务
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。