3

I am currently trying to implement a service that runs a special command that does something with arguments passed, for example: file paths. I am using the cmd command:

sc control "ServiceName" 128

However, this command doesn't provide any way for me to input arguments. The method is as below:

    protected override void OnCustomCommand(int command)
    {
        switch(command)
        {
            case 128:
                Command.StartProcess(3);
                LogWriter.WriteLog("Next output: ");
                Command.StartProcess(4);
                break;
            case 129:
                // input extension, output extension, key id , working path
                try
                {
                    test t1 = new test();
                    t1.readLog(@"C:\Users\Joe\Desktop\success.txt");
                    LogWriter.WriteLog(t1.input);
                    LogWriter.WriteLog(t1.output);
                }
                catch(Exception e)
                {
                    LogWriter.WriteLog(e.ToString());
                }
                finally { LogWriter.WriteLog("abc"); }
                //LogWriter.WriteLog(t1.output + "def");
                break;
        }
    }

The only argument i can input is the int command for the method. I would like to input a folder path for t1.readLog();. In the above code, I have to hard code the path which isn't flexible and troublesome. So, is there anyway to work this out?

1 Answers1

0

You are overriding the wrong method for this. Take a look at ServiceBase.OnStart. It gets array of arguments that can be set in command line like this:

sc start MyService arg1 arg2

Parse args array as you want:

protected override void OnStart(string[] args)
{
   var path = args[0];
   var someParameter = args[1];
}

Process initialization arguments for the service in the OnStart method, not in the Main method. The arguments in the args parameter array can be set manually in the properties window for the service in the Services console