NullReference
NullReference

Reputation: 2968

How to get the command-line arguments of a running windows service application?

We have a windows service application. When we install the service, we pass in command line arguments to capture some values that are used by the service. As an example, here -r is used to identify that it is an install.

 MyService.exe -r /url=value1 /time=value2

These values are stored in the local registry.

Now we want these values to be configurable. i.e, when we stop and start the service, we should be able to do something like (-s to identify start of the service)

 MyService.exe -s /time=newvalue

Now, is it possible to read this new value from the command line?.

Upvotes: 1

Views: 2470

Answers (2)

user1672994
user1672994

Reputation: 10849

There are two types of arguments for windows services :

  1. arguments that were passed on the process start command line. You can get to those easily using Process Explorer.
  2. arguments that were passed to the ServiceBase.OnStart function. This is what is used when service is started by SC START [arguments]. arguments passed in the method is different from "command line process arguments". So arguments passed in SC start is only known by the service itself.

Upvotes: 1

theB
theB

Reputation: 6738

Assuming that your entry point for the EXE looks something like

static void Main(string[] args)

You can just use the args array which has all the command line arguments as an array.

If you don't/can't get it in the entry point, you can use

string[] args = Environment.GetCommandLineArgs();

to get the command line arguments.

Upvotes: 3

Related Questions