我已成功将参数从Installutil传递到我的serviceinstaller,但我似乎无法将这些参数传递给Main(string [args]函数。 以下是我试图这样做的方法....如果有更好的办法做我正在做的事情请告诉我
protected override void OnAfterInstall(IDictionary savedState)
{
base.OnAfterInstall(savedState);
string[] args = new string[2];
args[0] = Context.Parameters["username"];
args[0] = Context.Parameters["password"];
new ServiceController(this.dataLoaderServiceInstaller.ServiceName).Start(args);
}
这是我的Program.cs
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter(@"c:\\bw\\date.txt");
// write a line of text to the file
tw.WriteLine(args.Length);
// close the stream
tw.Close();
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new DataloaderService()
};
ServiceBase.Run(ServicesToRun);
}
我试图写的句子长度总是为零。 计算机/服务器重新启动维护后,还有一个问题是这些参数是否仍然存在? 提前致谢。 :)
答案 0 :(得分:2)
使用ServiceController.Start()方法发送的参数可用作OnStart()方法的参数。如果我没有记错的话(我需要这么做一段时间)。
OnStart方法的签名是:
OnStart(string[] args)
但是,如果每次启动(自动)启动服务时都需要将参数发送到服务,那么您应该查看MSDN文档。具体地
处理初始化参数 OnStart方法中的服务,而不是 在Main方法中。中的论点 可以设置args参数数组 在属性窗口中手动执行 服务控制台中的服务。 在控制台中输入的参数 没有保存;他们被传递给了 服务一次性的时候 服务从控件启动 面板。必须存在的论据 当服务自动时 start可以放在ImagePath中 服务的字符串值 注册表项 (HKEY_LOCAL_MACHINE \系统\ CurrentControlSet \服务\)。您可以获取参数 从注册表中使用 GetCommandLineArgs方法,用于 示例:string [] imagePathArgs = Environment.GetCommandLineArgs();
http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx