Set 'Start Parameters' on Service Installation with .Net ServiceInstaller?

后端 未结 6 929
旧巷少年郎
旧巷少年郎 2020-12-16 02:47

I am currently writing a little windows service application and I can successfully in/uninstall it etc via something like this:

        serviceProcessInstall         


        
6条回答
  •  情深已故
    2020-12-16 03:23

    There is a managed way to add start parameters to a services (not in the "Start parameters"/"Startparameter" section of the management console (services.msc) but in "Path to executable"/"Pfad zur EXE-Datei" like all the windows' native services do).

    Add the following code to your subclass of System.Configuration.Install.Installer: (in C#-friendly VB-Code)

    'Just as sample
    Private _CommandLineArgs As String() = New String() {"/Debug", "/LogSection:Hello World"}
    
    ''' Command line arguments without double-quotes.
    Public Property CommandLineArgs() As String()
      Get
        Return _CommandLineArgs
      End Get
      Set(ByVal value As String())
        _CommandLineArgs = value
      End Set
    End Property
    
    Public Overrides Sub Install(ByVal aStateSaver As System.Collections.IDictionary)
      Dim myPath As String = GetPathToExecutable()
      Context.Parameters.Item("assemblypath") = myPath
      MyBase.Install(aStateSaver)
    End Sub
    
    Private Function GetPathToExecutable() As String
      'Format as something like 'MyService.exe" "/Test" "/LogSection:Hello World'
      'Hint: The base class (System.ServiceProcess.ServiceInstaller) adds simple-mindedly
      '      a double-quote around this string that's why we have to omit it here.
      Const myDelimiter As String = """ """ 'double-quote space double-quote
      Dim myResult As New StringBuilder(Context.Parameters.Item("assemblypath"))
      myResult.Append(myDelimiter)
      myResult.Append(Microsoft.VisualBasic.Strings.Join(CommandLineArgs, myDelimiter))
      Return myResult.ToString()
    End Function
    

    Have fun!

    chha

提交回复
热议问题