NEWS

[C#] Quản lý dịch vụ Windows Services trên Csharp Winform

[C#]  Quản lý dịch vụ Windows Services trên Csharp Winform
Đăng bởi: Thảo Meo - Lượt xem: 2057 08:55:04, 10/10/2022C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục chia sẻ đến các bạn source code Quản lý các dịch vụ đang chạy trên Window bằng Winform Csharp.

[C#] Control All Services in Windows System

Dưới đây, là giao diện demo ứng dụng Danh sách dịch vụ Windows bằng C#:

windows_services_csharp

Ở giao diện, mình có ba button: refresh, start và stop.

Khi các bạn bấm nút Refresh nó sẻ load tất cả các dịch vụ trên Windows vào Listview.

Bấm nút, start/stop để khởi động hoặc dừng một dịch vụ.

Full source code c# windows services:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ManagerWindowsServices
{
    public partial class Form1 : Form
    {
        private ServiceController serviceController = new ServiceController();
        public Form1()
        {
            InitializeComponent();
            StringExtensions.colorListViewHeader(ref lstServices, ColorTranslator.FromHtml("#c41d7f"), Color.White);
            lstServices.SelectedIndexChanged += LstServices_SelectedIndexChanged;
            lstServices.FullRowSelect = true;
        }

        private void LstServices_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstServices.SelectedItems.Count > 0)
                this.serviceController.ServiceName = lstServices.SelectedItems[0].SubItems[1].Text.Trim();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            GetServices();
        }

        private void GetServices()
        {
            try
            {
                lstServices.Items.Clear();

                ServiceController[] svcs = ServiceController.GetServices();

                foreach (ServiceController svc in svcs)
                {
                    ListViewItem item = new ListViewItem();
                    item.Text = svc.DisplayName;
                    item.SubItems.Add(svc.ServiceName);        
                    item.SubItems.Add(svc.Status.ToString());                 
                    lstServices.Items.Add(item);
                }

             
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            GetServices();
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!this.serviceController.CanStop)
            {
                this.serviceController.Start();
                this.GetServices();
            }
           
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            if (this.serviceController.CanStop)
            {
                this.serviceController.Stop();
                this.GetServices();
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#]  Quản lý dịch vụ Windows Services trên Csharp Winform
Đăng bởi: Thảo Meo - Lượt xem: 2057 08:55:04, 10/10/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.