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