How to get specific registry value from windows service without service name

19 Views Asked by At

I install my service with InstallUtil and pass the parameters servicename and param1

InstallUtil.exe /servicename="UniversalService1" /param1="[param value1]" <service assemby>.exe

In the first step during service insallation I read these paramaters and store the value of param1 in the registry as a subvalue of (HKLM\SYSTEM\CurrentControlSet\Services\UniversalService1\Parameters). This works.

public partial class MultiServiceInstaller : Installer
{
    string serviceName = "";
    string param1 = "";

    protected override void OnBeforeInstall(IDictionary savedState)
    {
        var sn = Context.Parameters["servicename"];
        if (!string.IsNullOrEmpty(sn))
        {
            serviceName = sn:
            serviceInstaller.ServiceName = serviceName;
            serviceInstaller.DisplayName = serviceName;
        }

        var p1 = Context.Parameters["param1"];
        if (!string.IsNullOrEmpty(p1))
        {
            param1 = p1;
        }
    }

    protected override void OnAfterInstall(IDictionary savedState)
    {
         base.OnAfterInstall(savedState);

         string keyParameters = string.Format("SYSTEM\\CurrentControlSet\\Services\\{0}\\Parameters", 
               serviceName);
 
         using (var swKey = Registry.LocalMachine.CreateSubKey(keyParameters))
         {
             swKey.SetValue("PARAM1", param1, RegistryValueKind.String);
         }
    }
}

In the second step during service start I want to read the parameter "PARAM1" from the registry to work with. But at this point I don't have the service name ans can not address the value.

public partial class Service : ServiceBase
{
     public Service()
     {
         InitializeComponent();
     }

     protected override void OnStart(string[] args)
     {
         // How to get registry values (PARAM1) without ServiceName here

         string keyParameters = string.Format("SYSTEM\\CurrentControlSet\\Services\\{0}\\Parameters", 
               [?????????]);

     }
}

Is there any way to get the service name?

Until now my only solution is to store the parameter in a config file instead the registry.

0

There are 0 best solutions below