How to pass parameters to a Windows service from Installer to Main in Program.cs?

I successfully passed parameters from Installutil to my service technician, but I cannot pass these parameters to Main (string [args]). This is how I try to do it .... if there is a better way to do what I do, please let me know

    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);
    }

and this is my 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);
    }

the length of the strokes I am trying to record is always zero. One more question will this parameter remain after rebooting the computer / server for maintenance? Thanks in advance.:)

+3
source share
1 answer

, ServiceController.Start(), OnStart(). ( - , ).

OnStart:

OnStart(string[] args)

, , () , MSDN .

OnStart, . args . , ; , . , ImagePath (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services \). , GetCommandLineArgs : string [] imagePathArgs = Environment.GetCommandLineArgs();.

http://msdn.microsoft.com/en-us/library/system.serviceprocess.servicebase.onstart.aspx

+2

Source: https://habr.com/ru/post/1793347/


All Articles