欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中调用Windows系统服务exe程序的工具类与重启服务的流程

程序员文章站 2023-11-14 18:07:04
场景 使用C#编写的Windows服务程序,在Winform中进行调用。 常用工具类方法检测服务是否存在或者安装,获取服务状态,启动服务,停止服务的方法。 以在Winform中重启服务为例。 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公 ......

场景

使用c#编写的windows服务程序,在winform中进行调用。

常用工具类方法检测服务是否存在或者安装,获取服务状态,启动服务,停止服务的方法。

以在winform中重启服务为例。

注:

博客主页:

关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载

实现

新建工具类winservicehelper

检测服务是否安装或者存在的方法

 

       /// <summary>
        /// 服务是否安装/存在
        /// </summary>
        /// <param name="servicename">服务名</param>
        /// <returns></returns>
        public static bool isserviceinstalled(string servicename)
        {
            bool exists = false;
            system.serviceprocess.servicecontroller[] services = system.serviceprocess.servicecontroller.getservices();
            foreach (system.serviceprocess.servicecontroller s in services)
            {
                if (s.servicename == servicename)
                {
                    exists = true;
                    break;
                }
            }
            return exists;
        }

 

获取服务状态的方法

        /// <summary>
        /// 获取服务状态
        /// </summary>
        /// <param name="servicename"></param>
        /// <returns></returns>
        public static string getservicestatus(string servicename)
        {
            string result = "服务不存在";
            system.serviceprocess.servicecontroller[] services = system.serviceprocess.servicecontroller.getservices();
            foreach (system.serviceprocess.servicecontroller s in services)
            {
                if (s.servicename == servicename)
                {
                    result = s.status.tostring();
                    break;
                }
            }
            return result;
        }

 

注:

服务状态返回值是枚举类型,具体返回值如下

 

   // 摘要: 
    //     指示服务的当前状态。
    public enum servicecontrollerstatus
    {
        // 摘要: 
        //     服务未运行。这对应于 win32 service_stopped 常数,该常数定义为 0x00000001。
        stopped = 1,
        //
        // 摘要: 
        //     服务正在启动。这对应于 win32 service_start_pending 常数,该常数定义为 0x00000002。
        startpending = 2,
        //
        // 摘要: 
        //     服务正在停止。这对应于 win32 service_stop_pending 常数,该常数定义为 0x00000003。
        stoppending = 3,
        //
        // 摘要: 
        //     服务正在运行。这对应于 win32 service_running 常数,该常数定义为 0x00000004。
        running = 4,
        //
        // 摘要: 
        //     服务即将继续。这对应于 win32 service_continue_pending 常数,该常数定义为 0x00000005。
        continuepending = 5,
        //
        // 摘要: 
        //     服务即将暂停。这对应于 win32 service_pause_pending 常数,该常数定义为 0x00000006。
        pausepending = 6,
        //
        // 摘要: 
        //     服务已暂停。这对应于 win32 service_paused 常数,该常数定义为 0x00000007。
        paused = 7,
    }

 

启动服务的方法

 

       /// <summary>
        /// 启动服务
        /// </summary>
        /// <param name="serivceexefullpath">服务全路径</param>
        /// <param name="servicename">服务名</param>
        /// <returns></returns>
        public static bool servicestart(string serivceexefullpath ,string servicename)
        {
            if (!isserviceinstalled(servicename))
            {
                messagebox.show("服务未安装,请先安装!");
                return false;
            }
            try
            {
                using (system.diagnostics.process p = new system.diagnostics.process())
                {
                    p.startinfo.useshellexecute = false;
                    p.startinfo.redirectstandardoutput = true;
                    p.startinfo.createnowindow = true;
                    p.startinfo.filename = serivceexefullpath;
                    p.startinfo.arguments = "start";
                    p.start();
                    p.close();
                }
                system.threading.thread.sleep(2000);
                return true;
            }
            catch (exception ex)
            {
                messagebox.show("服务安装异常:" + ex.message);
                return false;
            }
        }

 

停止服务的方法

        /// <summary>
        ///  停止服务
        /// </summary>
        /// <param name="serivceexefullpath">服务全路径</param>
        /// <param name="servicename">服务名</param>
        /// <returns></returns>
        public static bool servicestop(string serivceexefullpath, string servicename)
        {
            if (!isserviceinstalled(servicename))
            {
                messagebox.show("服务未安装,请先安装!");
                return false;
            }
            try
            {
                using (system.diagnostics.process p = new system.diagnostics.process())
                {
                    p.startinfo.useshellexecute = false;
                    p.startinfo.redirectstandardinput = true;
                    p.startinfo.createnowindow = true;
                    p.startinfo.filename = serivceexefullpath;
                    p.startinfo.arguments = "stop";
                    p.start();
                    p.waitforexit();
                    p.close();
                }
                system.threading.thread.sleep(2000);
                return true;
            }
            catch (exception ex)
            {
                messagebox.show("服务停止异常:" + ex.message);
                return false;
            }
        }

 

重启服务示例

在重启服务的按钮的点击事件中

 

     //检测服务是否安装
            bool isinstalled = winservicehelper.isserviceinstalled(global.bts_data_service_name);
            if (!isinstalled)
            {
                messagebox.show("重启失败,服务"+global.bts_data_service_name+"未安装或未启动");
                return;
            }
            string servicestatus = winservicehelper.getservicestatus(global.bts_data_service_name);
            if (!servicestatus.equals(system.serviceprocess.servicecontrollerstatus.running.tostring()))
            {
                messagebox.show("重启失败,服务" + global.bts_data_service_name + "状态为:" + servicestatus);
                return;
            }
            string serivceexefullpath = global.appconfig.btsdataserviceexe;
            string servicename = global.bts_data_service_name;
            bool isstopsuccess = winservicehelper.servicestop(serivceexefullpath,servicename);
            //停止失败
            if (!isstopsuccess)
            {
                messagebox.show("重启失败,服务" + global.bts_data_service_name + "停止失败");
                return;
            }
            //方法里已经休眠2秒
            bool isstartsuccess = winservicehelper.servicestart(serivceexefullpath, servicename);
            //启动失败
            if (!isstartsuccess)
            {
                messagebox.show("重启失败,服务" + global.bts_data_service_name + "启动失败");
                return;
            }
            messagebox.show("服务" + global.bts_data_service_name + "重启成功");