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

1 comment: