How to get the windows service name within a windows service writting in C# derived from ServiceBase

Posted on 2011-05-06
Last Modified: 2013-12-16
Hi folks

I'm writing a windows service using VS2010 and C# 4.0. The idea is to have multiple windows services installed using the same executable. To identify the current instance, I need to figure out the current service name of the instance.

The template in VS2010 derives the service from

Open in new window

. Who thinks that the property ServiceName in ServiceBase points to the according SCM service name, is wrong. ServiceName always contains the fixed string entered in the property dialog when creating the service. Removing it doesn't help either.


Open in new window

returns a list of all services (as a ServiceController object) on the machine. The ServiceController contains a property ServiceName as well, which contains also the name I'm looking for, but I don't know which ServiceController matches my service. Actually the name is exactly what I'm looking for. And if I have multiple services from the same executable, I cannot distinguishe them.

Does anybody have an idea how to get the name of the current service?

Question by:Thomy-M
    LVL 7

    Expert Comment

    You can use  process initialization arguments to identify type of service.
    Startup arguments for service are obtained in OnStart method.

     See more at:
    LVL 1

    Author Comment

    As far as I know args in OnStart gets the parameters specified in the start parameter textbox, but how can they be persisted? I install the service using InstallUtil.exe, I created a project installer that sets several parameters, e.g. service name.
    LVL 7

    Accepted Solution

    As you can see in documentation, to add parameters that will always be present you must edit ImagePath in registry for your service

     (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<service name>\ImagePath).

    You can do this during installation of your service.

    LVL 1

    Author Comment

    This hint was what I needed, this seem so work. During installation the parameters have to be written in the ImagePath of the service reg key.

    public override void Install(IDictionary stateSaver)
    	string serviceInstance = this.Context.Parameters["serviceInstance"] as string;
    	string serviceRegistryPath = "System\\CurrentControlSet\\Services\\" + this.serviceInstaller.ServiceName;
    	RegistryKey servicesKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(serviceRegistryPath, true);
    	// Should never happen
    	if (servicesKey == null)
    		throw new InstallException("Service key not found in the registry [" + serviceRegistryPath + "]");
    	string imagePath = (string)servicesKey.GetValue("ImagePath");
    	// Should never happen
    	if (imagePath == null)
    		throw new InstallException("ImagePath not found in the registry [" + serviceRegistryPath + ".ImagePath]");
    	imagePath += " -instance " + serviceInstance;
    	servicesKey.SetValue("ImagePath", imagePath);

    Open in new window

    See also

    Another possibility to figure out the current service configuration is to compare the entries in ServiceController.GetServices() with the current ProcessID. For this, you need to lookup the service process PID using a ManagementObject.

    private string getCurrentServiceName()
    	Process prc = Process.GetCurrentProcess();
    	List<ServiceController> dswServices = ServiceController.GetServices()
    		.Where(s => s.ServiceName.StartsWith("myService_") && s.Status != ServiceControllerStatus.Stopped)
    	foreach (var s in dswServices)
    		ManagementObject service = new ManagementObject(@"Win32_service.Name='" + s.ServiceName + "'");
    		object o = service.GetPropertyValue("ProcessId");
    		int processId = (int)((UInt32)o);
    		if (processId == prc.Id) return s.ServiceName;
    	return null;

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now