We have a Windows service that we write ourselves, and we want the service to implement different functions at startup, depending on the parameters entered by the user.
It is not difficult to achieve such a requirement, the following example I used to demonstrate how to write the service
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Diagnostics;6 usingSystem.Linq;7 usingsystem.serviceprocess;8 usingSystem.Text;9 Ten namespaceWindowsservicesample One { A Public Partial classSampleservice:servicebase - { - PublicSampleservice () the { - InitializeComponent (); - } - + /// <summary> - ///triggered when service starts + /// </summary> A /// <param name= "args" >input parameters that the user can set</param> at protected Override voidOnStart (string[] args) - { - - if(args. Length >0) - { -EventLog.WriteEntry (string. Format ("Sample service is started with parameter: {0}",string. Join (",", args )); in } - Else to { +EventLog.WriteEntry ("Sample service is started with non parameter"); - } the } * $ protected Override voidOnStop ()Panax Notoginseng { - } the } +}
In the above code, I distinguish between starting with a parameter and starting without a parameter. Obviously, in the case with parameters, we can do different processing according to the parameters. In order to illustrate the problem of parameters, this article does not really consider those practical applications. I can tell by the way I write a log.
After the service is installed, it looks like this in the service console
If we start by hand, how do we set the parameters? Very simple, double-click the service and enter parameters in the Properties dialog box
"Note" is to enter the parameters first, then click "Start"
"Note" The different parameters are separated by a space
After successful startup, we can see a log in the event log
This means that the parameters are actually captured. The service is working properly.
It's not bad, right? But the main purpose of this article is not to explain this, the above is the foreshadowing, if we are to start the service through the command line script, rather than manually start the service, then how to set the parameters?
We all know that to start a service on the command line, it's generally through the net start command. The format of this tool is as follows
We can start the service via net start Sampleservice
You do see in the event log that it was started without parameters
Okay, so how do you pass parameters past?
After research has found that net tools to pass parameters, there is a very special practice
net start sampleservice/a/b/c/D
But after this startup, look at the log and find a little bit special
Please main, the parameters are actually passed in. But it contains the "/". If this particular symbol is identified in the program, then there is no problem. It's just that it doesn't necessarily add to the complexity of the program.
So, is there a simpler approach? (simple means that it is the same as manual operation, do not need to modify the code for this)
I remember that, in addition to using net start, you can start the service with the SC start command.
Such a command is much more intuitive. Let's look at the results in the log file right away.
Well, that's the effect we need. In other words, with the SC tool, parameters can be passed well.
"Remarks" SC means, service controller, for a detailed description of the tool, please refer to the http://www.microsoft.com/resources/documentation/windows/xp/all/ Proddocs/en-us/sc.mspx?mfr=true
"Go" How to start a Windows service with parameters in a command-line script