您的位置:首页 > 其它

给Windows 服务添加命令行参数

2008-05-21 12:56 134 查看
1. 首先,给服务的Main方法添加参数,判断如果参数为"-s"则运行服务。

static void Main(string[] args)

{

// 运行服务

if (args[0].ToLower() == "/s" || args[0].ToLower() == "-s")

{

ServiceBase[] ServicesToRun;

ServicesToRun = new ServiceBase[] { new MyService1() };

ServiceBase.Run(ServicesToRun);

}

}

2. 然后,给 serviceInstaller1 添加 AfterInstall 事件,修改注册表给服务添加命令行参数。

private void ServiceInstaller1_AfterInstall(object sender, InstallEventArgs e)

{

// 给服务添加命令行参数 -s

try

{

RegistryKey regKey = Registry.LocalMachine.OpenSubKey

(@"SYSTEM\CurrentControlSet\Services\" + serviceInstaller1.ServiceName, true);

object value = regKey.GetValue("ImagePath");

if (value != null)

{

string imagePath = value.ToString();

regKey.SetValue("ImagePath", imagePath + " -s");

regKey.Flush();

}

regKey.Close();

}

catch

{}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: