views:

773

answers:

1

Windows Services can have parameters in the registry like in

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\BITS\Parameters

I want to access the parameters of my own service written in C#. Is there an official way (e.g. via the ServiceBase class) to do that or do I have to hard-code the registry path, which I want to avoid?

A: 

Are you looking for the paramenters the server WAS started with, or what the registry is saying it should be started with?

If your looking for the parameters that was used to start your app you can use Environment.CommandLine or make surge your main function is declaired like

[STAThread]
        static void Main(string[] args)
        {
            // Do something with args array, but il make sure its not null first.
        }

If your are looking for what args it should be started with i see no problem reading that registry setting.

EKS
Hi,thanks for the answer. However, I do not think that the (command line) parameters passed as arguments are equivalent to the parameters stored in the Parameters registry key. At least, it did not turn out to be so in my test.
Christoph
No, they are not equivilent. The only time the service is started with actual parameters passed to its ServiceMain() handler is when the user typed in startup parameters in the SCM's UI and then pressed the Start button, or when another app calls the Win32 API StartService() function with its lpServiceArgVectors parameter filled in. As for passing command-line parameters (which are different than service startup parameters), they have to be specified in the "ImagePath" value of the service's main Registry key, and thus get passed in when the SCM starts a new process for the service.
Remy Lebeau - TeamB