Click here to Skip to main content
15,908,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

Name of current service in case the same exe was setup as multiple services

Is there a way to get the service name from C# that the current service was registered with when it was installed.

For instance, I register the same service twice:
sc.exe create ServiceName1 binPath= D:\myservice.exe
sc.exe create ServiceName2 binPath= D:\myservice.exe

I have installed both services. I want to kill sc.exe of ServiceName1, how to check whether sc.exe belongs to ServiceName1.

Please help me.


Thanks,
-RG
Posted

1 solution

You'll want to use the ServiceController class from System.ServiceProcess. A simple example:

C#
using System;
using System.ServiceProcess;

public class ServiceUtil
{
    public static void StopService(string pName)
    {
        try
        {
            ServiceController sc = new (pName, Environment.MachineName);
            if (sc.Status == ServiceControllerStatus.Running) sc.Stop();
        }

        catch (ArgumentException)
        {
            // service does not exist
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900