1

I'm using this method: How to make a .NET Windows Service start right after the installation?

The post starts with "posted a step-by-step procedure". How can I pass parameters to the OnStart function without using registry?

I can pass parameters on this method Main(string[] args). I'm calling myapp.exe -install. I want to call myapp.exe -install path="c:/bla bla".

And also move the path parameter to "OnStart". My OnStart exist on the YourService object in the example.

Community
  • 1
  • 1
avnic
  • 3,241
  • 8
  • 37
  • 46

2 Answers2

1

From http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx...

The arguments in the args parameter array can be set manually in the properties window for the service in the Services console. The arguments entered in the console are not saved; they are passed to the service on a one-time basis when the service is started from the control panel. Arguments that must be present when the service is automatically started can be placed in the ImagePath string value for the service's registry key (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\). You can obtain the arguments from the registry using the GetCommandLineArgs method, for example: string[] imagePathArgs = Environment.GetCommandLineArgs();.

Using your example, you'd want to put -install path="c:/bla bla" in the Services console, but this is hardly satisfactory since it's not going to be saved, i.e., you'd have to do it every time your service started. You could go the registry route, but you said you don't want to do that. The only other option that comes to mind is some kind of service configuration file.

Matt Davis
  • 45,297
  • 16
  • 93
  • 124
0

you would pass the application name myapp.exe /i then in your params check check to see if /i was in the command line args within that code you assign the application path you could propbably pass the path as well surrounded by " " do a google search on passing params to a console application.. same theory applies..

MethodMan
  • 18,625
  • 6
  • 34
  • 52