Create a combo command line / Windows service app

前端 未结 4 594
说谎
说谎 2021-02-02 16:19

What\'s the best way in C# to set up a utility app that can be run from the command line and produce some output (or write to a file), but that could be run as a Windows service

4条回答
  •  借酒劲吻你
    2021-02-02 16:53

    Yes it can be done.

    Your startup class must extend ServiceBase.

    You could use your static void Main(string[] args) startup method to parse a command line switch to run in console mode.

    Something like:

    static void Main(string[] args)
    {
       if ( args == "blah") 
       {
          MyService();
       } 
       else 
       {
          System.ServiceProcess.ServiceBase[] ServicesToRun;
          ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
          System.ServiceProcess.ServiceBase.Run(ServicesToRun);
       }
    

提交回复
热议问题