Search This Blog

Friday, February 21, 2014

Get all services C#

C# > ServiceProcess > Service Controller

It is a Windows service that allows you to connect to a service.
You can manipulate it or get information about it.

Example:
Get list of all 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 WindowsFormsApplication1
{
    public class clsService
    {
        public string Name { get; set; }
        public string Type { get; set; }
        public string Status { get; set; }
    }
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            ServiceController[] scServices;
            scServices = ServiceController.GetServices();
            List<clsService> lcs = new List<clsService>();
            foreach (ServiceController sc in scServices)
            {
                clsService cs = new clsService();
                cs.Name = sc.ServiceName;
                cs.Type = sc.ServiceType.ToString();
                cs.Status = sc.Status.ToString();
               
                lcs.Add(cs);
            }
            dgv.DataSource =lcs ;
        }
    }
}