Send string commands or bytes to a Windows Service? (When running)

后端 未结 4 1283
予麋鹿
予麋鹿 2021-02-10 22:41

Is there any way to give string or byte array commands to a Windows Service? (When running)

4条回答
  •  后悔当初
    2021-02-10 23:06

    You can send start, restart and stop commands to the service, and I belive you can execute other defined commands as well.

    Here is an example of programatically starting a registered service called "myService"

    using System.ServiceProcess;
    //...
                System.ServiceProcess.ServiceController Service;
                Service = new ServiceController("myService");
                if (Service != null && Service.Status == ServiceControllerStatus.Stopped)
                        Service.Start();
    //...
    

    Look in SeviceController. You are probably looking for:

        //
        // Summary:
        //     Executes a custom command on the service.
        //
        public void ExecuteCommand(int command);
    

    It's my understanding though that these commands need to be predefined. You cannot pass arbrary commands to the services, i.e., this is not a method of passing data.

提交回复
热议问题