コード例 #1
0
        //获取服务状态
        void GetServiceStatus(string serviceName, Label txtStatus)
        {
            try
            {
                if (ServiceAPI.isServiceIsExisted(serviceName))
                {
                    string statusStr = "";
                    int    status    = ServiceAPI.GetServiceStatus(serviceName);
                    switch (status)
                    {
                    case 1:
                        statusStr = "服务未运行!";
                        break;

                    case 2:
                        statusStr = "服务正在启动!";
                        break;

                    case 3:
                        statusStr = "服务正在停止!";
                        break;

                    case 4:
                        statusStr = "服务正在运行!";
                        break;

                    case 5:
                        statusStr = "服务即将继续!";
                        break;

                    case 6:
                        statusStr = "服务即将暂停!";
                        break;

                    case 7:
                        statusStr = "服务已暂停!";
                        break;

                    default:
                        statusStr = "未知状态!";
                        break;
                    }
                    txtStatus.Text = statusStr;
                }
                else
                {
                    txtStatus.Text = "服务【" + serviceName + "】未安装!";
                }
            }
            catch (Exception ex)
            {
                txtStatus.Text = "error";
                LogAPI.WriteLog(ex.Message);
            }
        }
コード例 #2
0
        /// <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.InstallmyService(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
                {
                    if (btnStartOrEnd.Text == "停止服务")
                    {
                        MessageBox.Show("请先停止服务", "提示");
                        btnStartOrEnd.Focus();
                        return;
                    }
                    ServiceAPI.UnInstallmyService(serviceFileName);
                    if (!ServiceAPI.isServiceIsExisted(serviceName))
                    {
                        txtMsg.Text   = "服务【" + serviceName + "】卸载成功!";
                        btnOn.Enabled = btnShow.Enabled = false;
                        btnSet.Text   = "安装服务";
                        //gb.Text =strServiceName;
                        gb.Text = "融合平台工具管理服务";
                    }
                    else
                    {
                        txtMsg.Text = "服务【" + serviceName + "】卸载失败,请检查日志!";
                    }
                }
            }
            catch (Exception ex)
            {
                txtMsg.Text = "error";
                LogAPI.WriteLog(ex.Message);
            }
        }
コード例 #3
0
        /// <summary>
        /// 获取服务状态
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>返回服务状态</returns>
        public static int GetServiceStatus(string NameService)
        {
            int ret = 0;

            try
            {
                ServiceController sc = new ServiceController(NameService);
                ret = Convert.ToInt16(sc.Status);
            }
            catch (Exception ex)
            {
                ret = 0;
                LogAPI.WriteLog(ex.Message);
            }

            return(ret);
        }
コード例 #4
0
        /// <summary>
        /// 停止服务
        /// </summary>
        /// <param name=" NameService ">服务名</param>
        /// <returns>存在返回 true,否则返回 false;</returns>
        public static bool StopService(string NameService)
        {
            bool bo = true;

            try
            {
                ServiceController sc = new ServiceController(NameService);
                if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
                {
                    sc.Stop();
                }
            }
            catch (Exception ex)
            {
                bo = false;
                LogAPI.WriteLog(ex.Message);
            }

            return(bo);
        }
コード例 #5
0
 /// <summary>
 /// 获取指定服务的版本号
 /// </summary>
 /// <param name="serviceName">服务名称</param>
 /// <returns></returns>
 public static string GetServiceVersion(string serviceName)
 {
     if (string.IsNullOrEmpty(serviceName))
     {
         return(string.Empty);
     }
     try
     {
         string       path         = GetWindowsServiceInstallPath(serviceName) + "\\" + serviceName + ".exe";
         Assembly     assembly     = Assembly.LoadFile(path);
         AssemblyName assemblyName = assembly.GetName();
         Version      version      = assemblyName.Version;
         return(version.ToString());
     }
     catch (Exception ex)
     {
         LogAPI.WriteLog(ex.Message);
         return(string.Empty);
     }
 }
コード例 #6
0
        //启动服务
        void OnService(string serviceName, Button btn, Label txt)
        {
            try
            {
                if (btn.Text == "启动服务")//启动服务
                {
                    ServiceAPI.RunService(serviceName);

                    int status = ServiceAPI.GetServiceStatus(serviceName);
                    if (status == 2 || status == 4 || status == 5)
                    {
                        txt.Text = "服务【" + serviceName + "】启动成功!";
                        btn.Text = "停止服务";
                    }
                    else
                    {
                        txt.Text = "服务【" + serviceName + "】启动失败!";
                    }
                }
                else //关闭服务
                {
                    ServiceAPI.StopService(serviceName);

                    int status = ServiceAPI.GetServiceStatus(serviceName);
                    if (status == 1 || status == 3 || status == 6 || status == 7)
                    {
                        txt.Text = "服务【" + serviceName + "】停止成功!";
                        btn.Text = "启动服务";
                    }
                    else
                    {
                        txt.Text = "服务【" + serviceName + "】停止失败!";
                    }
                }
            }
            catch (Exception ex)
            {
                txt.Text = "error";
                LogAPI.WriteLog(ex.Message);
            }
        }
コード例 #7
0
        /// <summary>
        /// 获取服务安装路径
        /// </summary>
        /// <param name="ServiceName"></param>
        /// <returns></returns>
        public static string GetWindowsServiceInstallPath(string ServiceName)
        {
            string path = "";

            try
            {
                string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
                path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();

                path = path.Replace("\"", string.Empty);//替换掉双引号

                FileInfo fi = new FileInfo(path);
                path = fi.Directory.ToString();
            }
            catch (Exception ex)
            {
                path = "";
                LogAPI.WriteLog(ex.Message);
            }
            return(path);
        }
コード例 #8
0
        /// <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);
            }
        }