天天看点

C#小技巧系列之二:获取系统所有服务信息

说明:本人准备写一些C#有关的小技巧系列文章,这些文章含金量并不高,代码难度不大,不过因为问的次数比较多,从而导致本人决定用自己所知的方式写这一系列文章,可以看做“趣味导学”系列吧。

这是一个获取系统所有服务的程序,为了简化程序,代码仍以控制台形式提供,大家可以另外封装自己希望的功能。

核心代码:

using System;

using System.Collections.Generic;

using System.Text;

using System.ServiceProcess;

namespace Study

...{

    public class ServiceInfo

    ...{

        public void ShowServiceInfo()

        ...{

            ServiceController[] services=System.ServiceProcess.ServiceController.GetServices();

            System.Console.WriteLine("Service名称 Service显示名称 Service状态");

            for (int i = 0; i < services.Length; i++)

            ...{

                System.Console.WriteLine(string.Format("{0} {1} {2}", services[i].ServiceName, services[i].DisplayName, services[i].Status.ToString()));

            }

        }

    }

}

 启动代码:

    class Program

        static void Main(string[] args)

            ServiceInfo serviceInfo = new ServiceInfo();

            serviceInfo.ShowServiceInfo();

            Console.ReadLine();