开发者

Windows service - supplying arguments in "path to executable"

I cannot figure out how to pass (constant) arguments into my Windows service when it is started. I'm using the standard .NET classes like ServiceBase to implement (and ServiceProcessInstaller and ServiceInstaller to install) my service.

On the general tab of a Windows Service properties dialog box (once installed), there's a "Path to executable" in which I can see that some of the standard Windows services have command line arguments specified. System.开发者_如何学CServiceProcess.ServiceBase.OnStart takes string[] args, which I presume would enable these arguments to be accessed from within .NET code.

Are there some properties on ServiceProcessInstaller or ServiceInstaller that I can set to allow me to pass startup arguments to my own service, or does anyone know how it's supposed to be done?


Well, it's about 10 months after you post and I had the same issue. Once I read this thread and others, I decided to see what the .NET Framework is doing behind the scenes and verified that there is no documented way to do this. There is; however, a very simple undocumented way of doing this. In the parent Installer class, either override the Install method, or implement the BeforeInstall event. It doesn't matter which, you can use either of the following:

public override void Install(System.Collections.IDictionary stateSaver)
{
    base.Install(stateSaver);

    base.Context.Parameters["assemblyPath"] = string.Format("\"{0}\" /service", base.Context.Parameters["assemblyPath"]);
}

or:

private void Installer_BeforeInstall(object sender, InstallEventArgs e)
{
    base.Context.Parameters["assemblyPath"] = string.Format("\"{0}\" /service", base.Context.Parameters["assemblyPath"]);
}

The "assemblyPath" parameter will be initialized with the full path to the executable from within the AssemblyInstaller class. The value of this parameter is written to the ImagePath value in the registry. Quotes will be placed around this value if none exist, so make sure you place them where you need them (i.e. around the initial value at least). In the line above, I placed quotes around the execution file path and added " /service" to the end as a parameter.

This will apply to all services that use that parent Installer class. If you have services that you either don't want the parameters added to, or that need different parameters, you can nest Installer classes and place the code on those instead of the root Installer class. This isn't documented anywhere, could possibly break in the future, and may not work on non-Microsoft versions of the .NET Framework, but it does work for now.

I hope this helps.


The OnStart() arguments are supplied when the user starts the service by hand using the sc.exe start command from the command line. Or it can be done programmatically with the ServiceControl.Start(string[]) method overload. This is rarely useful, you probably want your service to start automatically without the requiring the user to logon.

Yes, the ImagePath registry key does support passing arguments to the .exe, you'll get them through the Main(string[]) entrypoint. Unfortunately, ServiceInstaller doesn't support this. A better way would be to use the registry. In your installer, create the HKLM\System\CurrentControlSet\services\yourServiceName\Parameters key and write values to it. And read them back in your service's Main or OnStart method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜