Click here to Skip to main content
15,867,594 members
Articles / Programming Languages / C#
Article

.NET Diagnostics - III, Enumerate System Services and Dependencies Using C#

Rate me:
Please Sign up or sign in to vote.
1.60/5 (4 votes)
18 Mar 2001 105.5K   773   19   12
How to get the list of all system services on a system and also finds out their dependencies.

Sample Image - NetDiagnostics3.jpg

Introduction

Continuing in the series of writing some diagnostics tools and exploring the System.Diagnostics namespace; this article shows how do you find out what all system services are running on the system and most importantly finding their dependencies. It's the later part that I found very interesting. Every time I open the Services dialog box or Task Manager on my system to look what all services are running, I used to think what will happen if I shut down one of them to recover some resources. But then you have to think, what if this service is part of some other service or application running on the system. This utility application enumerates and displays all the system services running on a system. And it also lists all the services a particular service is dependent upon and the services that depend on this service.

How do you do it?

The .NET framework provides the System.ServiceProcess namespace. This namespace contains classes for installing and running system services. In this namespace, you will find a class named SystemController. A SystemController is nothing but a System Service in NT lingo. This class has a static method GetServices() that returns an array of SystemController objects. This array contains all the services running on a system.

C#
ServiceController [] controllers = 
    ServiceController.GetServices();

After you have obtained this array, enumerate through each element to get details about each service. The SystemController class has a bunch of properties and their names are self-explanatory. I will not go into details of each property. I would suggest you to look in framework documentation for more details.

  • DisplayName - The descriptive name shown for this service in the Service applet.
  • Status - Status of the service e.g., Running, Stopped, etc.
  • ServiceType - Type of service that object references.
  • ServiceName  Short name of the service.
  • CanStop - Indicates if the service can be stopped at any time.
  • CanShutDown - Indicates if the service can be shut down any time.
  • CanPauseAndContinue - If the service can be stopped and continued.
  • DependentServices - List of services that depend on this service.
  • ServicesDependOn - List of services that this service depends on.

The ServiceController class has a couple of methods that can be used to start and stop a service. I have not shown the use of these methods in this utility project. But they are pretty simple to use.

Look at the InitList method in the source code attached with this article. This function shows how to use all the properties of the SystemController class.

C#
// Check the status of this service to see if 
// it can be stopped
bool bCanStop = controllers[i].CanStop;
item.SetSubItem(3, bCanStop ? "Yes" : "No");

// Check if the service recieves notification when 
// system shuts down.
bool bCanShutDown = controllers[i].CanShutdown;
item.SetSubItem(4, bCanShutDown ? "Yes" : "No");

// Check if the service can be paused and continued.
bool bPauseNContinue = controllers[i].CanPauseAndContinue;
item.SetSubItem(5, bPauseNContinue ? "Yes" : "No");

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
dudeua7-Dec-08 22:23
dudeua7-Dec-08 22:23 
QuestionProcess Id ? Pin
Alexis MICHEL16-Oct-07 5:10
Alexis MICHEL16-Oct-07 5:10 
Questionautomatic and manual Pin
jarellan12-Sep-07 23:40
jarellan12-Sep-07 23:40 
AnswerRe: automatic and manual Pin
AaronM_NZ27-Oct-07 15:19
AaronM_NZ27-Oct-07 15:19 
GeneralRe: automatic and manual Pin
gmooney26-Mar-08 5:37
gmooney26-Mar-08 5:37 
AnswerRe: automatic and manual Pin
AaronM_NZ7-Apr-08 0:57
AaronM_NZ7-Apr-08 0:57 
Questionwhere's the form ? Pin
Harkamal Singh12-Jul-07 0:56
Harkamal Singh12-Jul-07 0:56 
Questionhow to get image path Pin
Harkamal Singh12-Jul-07 0:52
Harkamal Singh12-Jul-07 0:52 
QuestionHow to implement in VC Pin
Prateeti22-Aug-02 18:54
Prateeti22-Aug-02 18:54 
GeneralStop time Pin
19-Mar-01 15:25
suss19-Mar-01 15:25 
QuestionHow to dynamic make a service interact with the desktop? Pin
19-Mar-01 15:21
suss19-Mar-01 15:21 
AnswerRe: How to dynamic make a service interact with the desktop? Pin
n4nilesh23-Aug-04 8:30
n4nilesh23-Aug-04 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.