winform实现系统服务管理(SSM)

c#编写的一个winform程序,实现对windows系统服务的管理。本程序可以对指定的windows服务,进行启动、停止、重新启动等操作。

设计界面:SSM.Designer.cs

namespace SSM
{
    partial class SSM
    {
        /// 
        /// 必需的设计器变量。
        /// 
        private System.ComponentModel.IContainer components = null;

        /// 
        /// 清理所有正在使用的资源。
        /// 
        /// 如果应释放托管资源,为 true;否则为 false。
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }



        #region Windows 窗体设计器生成的代码

        /// 
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// 
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.input = new System.Windows.Forms.TextBox();
            this.start = new System.Windows.Forms.Button();
            this.stop = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.output = new System.Windows.Forms.TextBox();
            this.restart = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(16, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(149, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "请输入要启动的服务名称:";
            // 
            // input
            // 
            this.input.BackColor = System.Drawing.Color.White;
            this.input.ForeColor = System.Drawing.SystemColors.WindowText;
            this.input.Location = new System.Drawing.Point(18, 39);
            this.input.Multiline = true;
            this.input.Name = "input";
            this.input.Size = new System.Drawing.Size(424, 122);
            this.input.TabIndex = 1;
            // 
            // start
            // 
            this.start.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(221)))), ((int)(((byte)(221)))), ((int)(((byte)(221)))));
            this.start.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.start.Location = new System.Drawing.Point(459, 38);
            this.start.Name = "start";
            this.start.Size = new System.Drawing.Size(85, 35);
            this.start.TabIndex = 2;
            this.start.Text = "启动";
            this.start.UseVisualStyleBackColor = true;
            this.start.Click += new System.EventHandler(this.start_Click);
            // 
            // stop
            // 
            this.stop.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.stop.Location = new System.Drawing.Point(459, 126);
            this.stop.Name = "stop";
            this.stop.Size = new System.Drawing.Size(85, 35);
            this.stop.TabIndex = 4;
            this.stop.Text = "停止";
            this.stop.UseVisualStyleBackColor = true;
            this.stop.Click += new System.EventHandler(this.stop_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.ForeColor = System.Drawing.Color.Gray;
            this.label3.Location = new System.Drawing.Point(16, 171);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(65, 12);
            this.label3.TabIndex = 5;
            this.label3.Text = "操作日志:";
            // 
            // output
            // 
            this.output.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(248)))), ((int)(((byte)(248)))), ((int)(((byte)(248)))));
            this.output.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.output.Enabled = false;
            this.output.ForeColor = System.Drawing.Color.Gray;
            this.output.Location = new System.Drawing.Point(18, 192);
            this.output.Multiline = true;
            this.output.Name = "output";
            this.output.Size = new System.Drawing.Size(424, 90);
            this.output.TabIndex = 6;
            // 
            // restart
            // 
            this.restart.FlatAppearance.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(221)))), ((int)(((byte)(221)))), ((int)(((byte)(221)))));
            this.restart.Font = new System.Drawing.Font("Arial", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.restart.Location = new System.Drawing.Point(459, 83);
            this.restart.Name = "restart";
            this.restart.Size = new System.Drawing.Size(85, 35);
            this.restart.TabIndex = 3;
            this.restart.Text = "重启";
            this.restart.UseVisualStyleBackColor = true;
            this.restart.Click += new System.EventHandler(this.restart_Click);
            // 
            // SSM
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(248)))), ((int)(((byte)(248)))), ((int)(((byte)(248)))));
            this.ClientSize = new System.Drawing.Size(559, 294);
            this.Controls.Add(this.restart);
            this.Controls.Add(this.output);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.stop);
            this.Controls.Add(this.start);
            this.Controls.Add(this.input);
            this.Controls.Add(this.label1);
            this.Name = "SSM";
            this.Text = "系统服务管理";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox input;
        private System.Windows.Forms.Button start;
        private System.Windows.Forms.Button stop;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.TextBox output;
        private System.Windows.Forms.Button restart;
    }
}

核心代码:SSM.cs

using System;
using System.ServiceProcess;
using System.Windows.Forms;

namespace SSM
{
    public partial class SSM : Form
    {
        public SSM()
        {
            InitializeComponent();
        }
        string val = "";
        DateTime logtime;
        private void start_Click(object sender, EventArgs e)
        {
            if (input.Text != "")
            {
                try
                {
                    string[] arr = new string[input.Lines.Length];
                    for (int i = 0; i < input.Lines.Length; i++)
                    {
                        val = input.Lines[i];
                        logtime = DateTime.Now;
                        ServiceController serviceObject = this.GetServiceObject(val);

                        this.StarService(serviceObject);
                        output.Text += logtime.ToString() + "\t" + val + "\t开启" + "\r\n";
                    }

                }
                catch (Exception ex)
                {
                    output.Text += "启动服务错误:" + ex.Message.ToString() + "\r\n";
                }
            }
            else
            {
                output.Text += "请输入要启动的服务..." + "\r\n";
            }

        }

        private void restart_Click(object sender, EventArgs e)
        {
            if (input.Text != "")
            {
                try
                {
                    string[] arr = new string[input.Lines.Length];
                    for (int i = 0; i < input.Lines.Length; i++)
                    {
                        val = input.Lines[i];
                        logtime = DateTime.Now;
                        ServiceController serviceObject = this.GetServiceObject(val);

                        this.ReStarService(serviceObject);
                        output.Text += logtime.ToString() + "\t" + val + "\t重启" + "\r\n";
                    }

                }
                catch (Exception ex)
                {
                    output.Text += "重启服务错误:" + ex.Message.ToString() + "\r\n";
                }
            }
            else
            {
                output.Text += "请输入要重启的服务..." + "\r\n";
            }

        }

        private void stop_Click(object sender, EventArgs e)
        {
            if (input.Text != "")
            {
                try
                {
                    string[] arr = new string[input.Lines.Length];
                    for (int i = 0; i < input.Lines.Length; i++)
                    {
                        val = input.Lines[i];
                        logtime = DateTime.Now;
                        ServiceController serviceObject = this.GetServiceObject(val);

                        this.StopService(serviceObject);
                        output.Text += logtime.ToString() + "\t" + val + "\t停止" + "\r\n";
                    }

                }
                catch (Exception ex)
                {
                    output.Text += "停止服务错误:" + ex.Message.ToString() + "\r\n";
                }
            }
            else
            {
                output.Text += "请出入要停止的服务..." + "\r\n";
            }

        }

        //获取windows服务实例
        private ServiceController GetServiceObject(string sc)
        {
            return new ServiceController(sc);
        }

        /// 
        /// 重启
        /// 
        /// 
        private void ReStarService(ServiceController sc)
        {
            this.StopService(sc);
            this.StarService(sc);
        }
        /// 
        ///  停止服务
        /// 
        /// 
        private void StopService(ServiceController sc)
        {
            if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Running))
            {
                sc.Stop();
            }
        }
        /// 
        /// 开始服务
        /// 
        /// 
        private void StarService(ServiceController sc)
        {
            if ((sc.Status == ServiceControllerStatus.Paused) || (sc.Status == ServiceControllerStatus.Stopped))
            {
                sc.Start();
            }
        }

    }
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注