zoukankan      html  css  js  c++  java
  • c#编写一个winform 管理一个windows服务

    使用c#编写一个winform程序, 管理一个windows服务。对指定的windows服务,进行启动、停止、重新启动

    1、引入 using System.ServiceProcess;

    2、

      /// <summary>
            /// 重新启动按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStopAndRun_Click(object sender, EventArgs e)
            {
                ServiceController serviceObject = this.GetServiceObject();
                try
                {
                    this.ReStarService(serviceObject);
                    MessageBox.Show("重启服务成功!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show("重启服务错误:" + ex.Message.ToString());
                }
            }
    
            //获取windows服务实例
            private ServiceController GetServiceObject()
            {
                return new ServiceController("FloatcarGpsAppServer");
            }
            /// <summary>
            /// 重启
            /// </summary>
            /// <param name="sc"></param>
            private void ReStarService(ServiceController sc)
            {
                this.StopService(sc);
                this.StarService(sc);
            }
            /// <summary>
            ///  停止服务
            /// </summary>
            /// <param name="sc"></param>
            private void StopService(ServiceController sc)
            {
                if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Running))
                {
                    sc.Stop();
                }
            }
            /// <summary>
            /// 开始服务
            /// </summary>
            /// <param name="sc"></param>
            private void StarService(ServiceController sc)
            {
                if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Stopped))
                {
                    sc.Start();
                }
            }
            /// <summary>
            /// 停止服务按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnStop_Click(object sender, EventArgs e)
            {
                ServiceController serviceObject = this.GetServiceObject();
                try
                {
                    this.StopService(serviceObject);
                    MessageBox.Show("停止服务成功!");
                }
                catch (Exception ex)
                {
    
                    MessageBox.Show("停止服务发生错误:" + ex.Message.ToString());
                }
            }
            /// <summary>
            /// 开始服务按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void btnRun_Click(object sender, EventArgs e)
            {
                ServiceController serviceObject = this.GetServiceObject();
                try
                {
                    this.StarService(serviceObject);
                    MessageBox.Show("启动服务成功!");
                }
                catch (Exception  ex)
                {
    
                    MessageBox.Show("启动服务失败:" + ex.Message);
                }
            }
    
  • 相关阅读:
    五、mariadb遇到的坑——Linux学习笔记
    四、CentOS 安装mariadb——Linux学习笔记
    [搬运] C# 这些年来受欢迎的特性
    [搬运] 写给 C# 开发人员的函数式编程
    [搬运]在C#使用.NET设计模式的新观点
    在容器中利用Nginx-proxy实现多域名的自动反向代理、免费SSL证书
    [翻译]在 .NET Core 中的并发编程
    [翻译] 使用ElasticSearch,Kibana,ASP.NET Core和Docker可视化数据
    .NET Core:使用ImageSharp跨平台处理图像
    .NET Core开源组件:后台任务利器之Hangfire
  • 原文地址:https://www.cnblogs.com/huacw/p/2106129.html
Copyright © 2011-2022 走看看