Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to register a .Net service with a command line + parameters?

I would like to be able to pass parameters in the installation of the service. I have modified the C# code of the class that inherit from Installer... My problem is the InstallUtil.exe doesn't work with parameters (well not as I know).

Any suggestion?

like image 699
Patrick Desjardins Avatar asked Jun 11 '09 19:06

Patrick Desjardins


People also ask

How do I pass command line arguments to Windows service?

Answers. You can add command line arguments to the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\[YourService]\ImagePath registry entry. These will get picked up by the Main() method in your service application. From there you can parse them and pass them to your service via properties, custom constructors, etc.

How do I change Windows service parameters?

Click the Start button in the Windows taskbar. In the menu, right-click Command Prompt. On the pop-up right click context menu, select Run as administrator. At the command prompt, enter the SC Config command with the service name to be modified and the parameters to be changed.


1 Answers

We have the same scenario and it works. You've to pass the parameters as follows

InstallUtil.exe /Param1="Value" /Param2="Value" "Path to your exe"

Then you've to override Install method on your installer

public override void Install(System.Collections.IDictionary stateSaver)
{
     var lParam1 =   GetParam("Param1");
}

private string GetParam(string pKey)
{
        try
        {
            if (this.Context != null)
            {
                if (this.Context.Parameters != null)
                {
                    string lParamValue = this.Context.Parameters[pKey];
                    if (lParamValue != null)
                        return lParamValue;
                }
            }
        }
        catch (Exception)
        {
        }
        return string.Empty;
    }
like image 54
Vasu Balakrishnan Avatar answered Sep 28 '22 02:09

Vasu Balakrishnan