Retrieve a List of the Services Running on your Computer using C# or VB.NET

The ServiceController class can be used to retrieve a list of the services running on your computer. The GetServices() can be used to do so. Here’s an example to list the services that are running on your machine

Add a reference to System.ServiceProcess.

image

Then write the following code:

C#

using System;
using System.ServiceProcess;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceController[] sController = ServiceController.GetServices();
foreach (ServiceController sc in sController)
{
if (sc.Status.ToString() == "Running")
{
Console.WriteLine(sc.ServiceName);
}
}
Console.ReadLine();
}
catch (Exception ex)
{
// handle ex
}
}
}
}

VB.NET

Imports System
Imports System.ServiceProcess

Namespace ConsoleApplication1
Friend Class Program
Shared Sub Main(ByVal args() As String)
Try
Dim
sController() As ServiceController = ServiceController.GetServices()
For Each sc As ServiceController In sController
If sc.Status.ToString() = "Running" Then
Console.WriteLine(sc.ServiceName)
End If
Next
sc
Console.ReadLine()
Catch ex As Exception
' handle ex
End Try
End Sub
End Class
End Namespace

OUTPUT

image



Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

addypam said...

Thanks for sharing this post