/// <summary> /// 卸载服务 /// </summary> /// <param name="serviceName"></param> public static bool UnInstallWindowService(string serviceName, ref string Msg) { try { if (ServiceIsExisted(serviceName)) { ServerAPIHelp.Uninstall(serviceName); Msg = "卸载服务成功!"; } else { Msg = "服务不存在!"; return(false); } return(true); } catch (Exception) { Msg = "卸载服务失败!"; throw; } }
/// <summary> /// 安装服务 /// </summary> public static bool InstallWindowService(ServerUtil service, ref string Msg) { if (!ServiceIsExisted(service.ServerName)) { //System.Configuration.Install.ManagedInstallerClass.InstallHelper(args); ServerAPIHelp.Install( service.ServerName, // 服务名 service.Name, // 显示名称 service.ServerPath, // 映像路径,可带参数,若路径有空格,需给路径(不含参数)套上双引号 service.ServerContent, // 服务描述 ServiceStartType.Auto, // 启动类型 ServiceAccount.LocalService, // 运行帐户,可选,默认是LocalSystem,即至尊帐户 null // 依赖服务,要填服务名称,没有则为null或空数组,可选 ); Msg = "服务安装成功!"; } else { Msg = "该服务已经存在,不用重复安装。"; return(false); } return(true); }