在过去的两个小时里,我一直在尝试使用sc.exe和pass参数来创建一个服务,但是我似乎无法正确地理解它。
我读过this SO question和所有的答案大约5次,这是没有帮助的!
根据我在这里所读到的,我似乎应该像这样构造这个命令:
sc create MyService binPath= "C:\path\to\myservice.exe --param1=TestString"我的服务OnStart方法如下所示:
Protected Overrides Sub OnStart(ByVal args() As String)
If Not IsNothing(args) Then
Library.WriteLog("Number of args = " & args.Count)
If args.Count > 0 Then
For i = 0 To args.Count - 1
Library.WriteLog("Arg" & i & ": " & args(i))
Next
End If
End If
End Sub但是,我尝试过的每一件事都产生了日志中的“Args =0的数目”。
为了清晰起见,我尝试了以下几点(再加上一些更可能的):
sc create MyService binPath= "C:\path\to\myservice.exe --param1=TestString"
sc create MyService binPath= "C:\path\to\myservice.exe --TestString"
sc create MyService binPath= "C:\path\to\myservice.exe --param1=\"TestString\""
sc create MyService binPath= "C:\path\to\myservice.exe -param1=TestString"
sc create MyService binPath= "C:\path\to\myservice.exe -TestString"
sc create MyService binPath= "C:\path\to\myservice.exe -param1=\"TestString\""我一定是漏掉了什么很傻的东西,但我正在用它把头撞到墙上!
发布于 2015-05-05 15:02:58
根据this answer and the comments,只有在windows服务对话框中手动设置启动参数时,才会使用OnStart的args参数,这些参数无法保存。
您可以通过访问Main方法中的参数(默认情况下位于Service.Designer.vb文件中)来使用正在设置的参数。以下是一个例子:
<MTAThread()> _
<System.Diagnostics.DebuggerNonUserCode()> _
Shared Sub Main(ByVal args As String())
Dim ServicesToRun() As System.ServiceProcess.ServiceBase
ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1(args)}
System.ServiceProcess.ServiceBase.Run(ServicesToRun)
End Sub您需要向服务类添加或修改构造函数以接受参数:
Private ReadOnly _arguments As String()
Public Sub New(ByVal args As String())
InitializeComponent()
_arguments = args
End Sub然后,您的OnStart方法变成:
Protected Overrides Sub OnStart(ByVal args() As String)
If Not IsNothing(args) Then
Library.WriteLog("Number of args = " & _arguments.Count)
If args.Count > 0 Then
For i = 0 To args.Count - 1
Library.WriteLog("Arg" & i & ": " & _arguments(i))
Next
End If
End If
End Subhttps://stackoverflow.com/questions/30045201
复制相似问题