How to pass parameters to Windows Service?

前端 未结 6 2614
猫巷女王i
猫巷女王i 2020-12-16 11:21

I tried to pass parameters to a windows service.

Here is my code snippet:

class Program : ServiceBase
{
    public String UserName { get; set; }
             


        
6条回答
  •  忘掉有多难
    2020-12-16 12:27

    You can pass parameters on startup like this:

    1. Right click on MyComputer and select Manage -> Services and Applications -> Services
    2. Right click on your service, select Properties and you should then see the Start Parameters box under the General tab.

    If you enter there for example User Password you will get these parameters in protected override void OnStart(string[] args) as args. then use it like this:

    protected override void OnStart(string[] args)
    {
        base.OnStart(args);
        UserName = args[0];
        Password = args[1];
        //do everything else
    }
    

提交回复
热议问题