Recently, I needed to create multiple instances of the window service for the same executable. Each instance need to perform different task depending upon the parameter being passed at the startup. Hence, the question is how do i pass the parameter to the service instance?
Within the Window Service project, Open the Program.cs class. Modify the Main method signature to take array of string as argument if it does not already.
static class Program
/// The main entry point for the application.
/// <param name="args">The list of arguments passed along with executable</param>
static void Main(string args)
string taskName = args;
ServiceBase ServicesToRun = new ServiceBase
As shown above, Main method takes the parameter and pass it to the Service class to perform the specific task.
Once the service is installed, Start Parameters can be passed on via Properties window of the Service. Goto Start > AdministrativeTools > Services window. Find the service in the list and right click on the service name. Click Properties from the menu to view Properties window. Under General tab at the bottom, you would see the text box for Start parameters. Type the parameter and click Start button.
Note: The start parameters passed via Properties textbox does not persist. Hence, for the service that need to start automatically, this won’t work.
Hence, we need to apply a different trick for services that need to persist startup parameter for auto start. For this, use the service registry key called ImagePath.
Goto Start > Run. Type Regedit and hit Enter. In the Registry Editor window, navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\<ServiceName>.
The ImagePath key contains the path to the executable as its value. Double click to edit the value. In the Value textbox, input the parameter after the path to executable with the space inbetween.
Value name: ImagePath
Value data: “Path to executable” “Parameter1” “Parameter2”
That is it.