【问题标题】:How to pass arguments to installutil MyService.exe Parameter1 Parameter2如何将参数传递给 installutil MyService.exe Parameter1 Parameter2
【发布时间】:2013-02-02 13:04:14
【问题描述】:

如何将参数传递给 installutil MyService.exe Parameter1 Parameter2

我的 MyService 是一个 Windows 服务。我想将参数传递给该服务。

【问题讨论】:

    标签: installutil


    【解决方案1】:

    覆盖 ProjectInstaller 类的 install 方法

    public override void Install(System.Collections.IDictionary stateSaver)
            {
                  base.Install(stateSaver);
                  string lParam1 = Convert.ToString(GetParam("Param1"));  
    
            }
    
            private object GetParam(string p)
            {
                try
                {
                    if (this.Context != null)
                    {
                        if (this.Context.Parameters != null)
                        {
                            string lParamValue = this.Context.Parameters[p];
                            if (lParamValue != null)
                                return lParamValue;
                        }
                    }
                }
                catch (Exception ex )
                {
    
                }
                return string.Empty;
            }
    

    并使用 installutil /Param1=value path_to_your_exename

    安装服务

    【讨论】:

      【解决方案2】:

      首先,您需要将安装程序插入到您的服务中。但我想你已经结束了。否则请阅读:

      https://docs.microsoft.com/en-us/dotnet/framework/windows-services/how-to-add-installers-to-your-service-application

      正如@Sumeshk 提到的,您可以使用 installutil 将参数传递给此安装程序,例如 installutil /Param1=value path_to_your_exename 并通过 this.Context.Parameters 在您的安装程序代码中检索这些信息。

      现在您想将这些参数作为参数传递给您的服务。诀窍是在安装之前将它们附加到assemblypath,如下所示:

      How to pass a parameter to a windows service once and for all at install instead of each start

      因此您必须覆盖安装程序方法OnBeforeInstall。然后,您可以通过附加参数来操作给定的组装路径。最后别忘了调用基本方法OnBeforeInstall

      protected override void OnBeforeInstall(IDictionary savedState)
      {
          string oldPath = this.Context.Parameters["assemblypath"];
          string yourParam = this.Context.Parameters["Param1"];
          string pathWithParam = $"\"{oldPath}\" \"{yourParam }\"";
          this.Context.Parameters["assemblypath"] = pathWithParam;
          base.OnBeforeInstall(savedState);
      }
      

      请注意,为避免参数或程序集路径中可能出现的空格问题,您应该将这些字符串用双引号括起来。用空格分隔每个参数。如果这样做,您的服务在注册表中的ImagePath 将由路径组成,并用空格分隔您的参数。你可以在密钥HKLM\SYSTEM\CurrentControlSet\Services\<your service name>中找到这个

      现在您可以在方法OnStartargs 参数中评估您的服务中的参数:

      protected override void OnStart(string[] args)
      {
          Debug.WriteLine("Param1: {args[0]}");
      }
      

      【讨论】:

        【解决方案3】:

        我对@Rouvens 帖子的评价太低了。我首先要感谢他的回答。不过,我想稍微更正一下。

        您通过安装程序组件为HKLM\SYSTEM\CurrentControlSet\Services\<your service name> 注册的参数不会像他声称的那样直接传递给OnStart(string[] args)

        相反,它们遵循服务的构造函数:

        public partial class MyService : ServiceBase
        {
            string myArgument;
        
            public MyService(string[] args)
            {            
                InitializeComponent();
                
                this.myArgument= {args[0]}; // this is where you capture your argument
            }
        }
        

        之后您必须在不考虑 OnStart 参数的情况下传递这些值。

        protected override void OnStart(string[] args)
        {
            BusinessLogicControl.Main(new string[] { myArgument});
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-05-22
          • 1970-01-01
          • 2020-10-09
          • 2012-12-21
          • 2012-01-07
          • 2014-05-03
          相关资源
          最近更新 更多