/// <summary> /// 安装或卸载服务 /// </summary> /// <param name="serviceName">服务名称</param> /// <param name="btnSet">安装、卸载</param> /// <param name="btnOn">启动、停止</param> /// <param name="txtMsg">提示信息</param> /// <param name="gb">组合框</param> void SetServerce(string serviceName, Button btnSet, Button btnOn, Button btnShow, Label txtMsg, GroupBox gb) { try { string location = System.Reflection.Assembly.GetExecutingAssembly().Location; string serviceFileName = location.Substring(0, location.LastIndexOf('\\')) + "\\" + serviceName + ".exe"; if (btnSet.Text == "安装服务") { ServiceApi.InstallService(null, serviceFileName); if (ServiceApi.IsServiceIsExisted(serviceName)) { txtMsg.Text = "服务【" + serviceName + "】安装成功!"; btnOn.Enabled = btnShow.Enabled = true; string temp = string.IsNullOrEmpty(ServiceApi.GetServiceVersion(serviceName)) ? string.Empty : "(" + ServiceApi.GetServiceVersion(serviceName) + ")"; gb.Text += temp; btnSet.Text = "卸载服务"; btnOn.Text = "启动服务"; } else { txtMsg.Text = "服务【" + serviceName + "】安装失败,请检查日志!"; } } else { ServiceApi.UnInstallService(serviceFileName); if (!ServiceApi.IsServiceIsExisted(serviceName)) { txtMsg.Text = "服务【" + serviceName + "】卸载成功!"; btnOn.Enabled = btnShow.Enabled = false; btnSet.Text = "安装服务"; //gb.Text =strServiceName; } else { txtMsg.Text = "服务【" + serviceName + "】卸载失败,请检查日志!"; } } } catch (Exception ex) { txtMsg.Text = "error"; LogApi.WriteLog(ex.Message); } }
/// <summary> /// 初始化控件状态 /// </summary> /// <param name="serviceName">服务名称</param> /// <param name="btn1">安装按钮</param> /// <param name="btn2">启动按钮</param> /// <param name="btn3">获取状态按钮</param> /// <param name="txt">提示信息</param> /// <param name="gb">服务所在组合框</param> void InitControlStatus(string serviceName, Button btn1, Button btn2, Button btn3, Label txt, GroupBox gb) { try { btn1.Enabled = true; if (ServiceApi.IsServiceIsExisted(serviceName)) { btn3.Enabled = true; btn2.Enabled = true; btn1.Text = "卸载服务"; int status = ServiceApi.GetServiceStatus(serviceName); if (status == 4) { btn2.Text = "停止服务"; } else { btn2.Text = "启动服务"; } GetServiceStatus(serviceName, txt); //获取服务版本 string temp = string.IsNullOrEmpty(ServiceApi.GetServiceVersion(serviceName)) ? string.Empty : "(" + ServiceApi.GetServiceVersion(serviceName) + ")"; gb.Text += temp; } else { btn1.Text = "安装服务"; btn2.Enabled = false; btn3.Enabled = false; txt.Text = "服务【" + serviceName + "】未安装!"; } } catch (Exception ex) { txt.Text = "error"; LogApi.WriteLog(ex.Message); } }