使用以下命令创建Windows服务时:
sc create ServiceName binPath= "the path"
如何将参数传递给Installer类的Context.Parameters集合?
我对sc.exe
文档的理解是,这样的参数只能在binPath
的末尾传递,但我还没有找到一个例子,也没有成功地做到这一点。
发布于 2011-01-29 05:59:40
我在Windows7上运行时遇到了问题,它似乎忽略了我传入的第一个参数,所以我使用了binPath= "C:\path\to\service.exe -bogusarg -realarg1 -realarg2"
,它成功了。
发布于 2012-07-25 17:01:36
我使用不带参数的方式创建它,然后编辑注册表HKLM\System\CurrentControlSet\Services\[YourService]
。
发布于 2017-02-27 20:33:28
考虑到如何访问应用程序代码中的参数也很重要。
在我的c#应用程序中,我使用了ServiceBase类:
class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
}
}
我使用以下命令注册了我的服务
sc create myService binpath= "MeyService.exe arg1 arg2“
但是当我将其作为服务运行时,我无法通过args
变量访问参数。
MSDN文档建议不要使用Main方法来检索binPath
或ImagePath
参数。相反,它建议将您的逻辑放在OnStart
方法中,然后使用(C#) Environment.GetCommandLineArgs();
。
要访问第一个参数arg1
,我需要这样做:
class MyService : ServiceBase
{
protected override void OnStart(string[] args)
{
log.Info("arg1 == "+Environment.GetCommandLineArgs()[1]);
}
}
这将打印出来
arg1 == arg1
https://stackoverflow.com/questions/3663331
复制相似问题