zoukankan      html  css  js  c++  java
  • 给Windows 服务添加命令行参数

    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
        {}
    }


  • 相关阅读:
    后缀自动机学习小记
    [bzoj4524] [loj#2047] [Cqoi2016] 伪光滑数
    [bzoj4825] [loj#2018] [Hnoi2017] 单旋
    [bzoj4571] [loj#2016] [Scoi2016] 美味
    [bzoj4569] [loj#2014] [Scoi2016] 萌萌哒
    [bzoj4568] [loj#2013] [Scoi2016] 幸运数字
    [bzoj4567] [loj#2012] [SCOI2016] 背单词
    deque双向队列
    STL_vector
    qsort与sort()
  • 原文地址:https://www.cnblogs.com/anjou/p/1203956.html
Copyright © 2011-2022 走看看