C# 服务无法启动。System.IO.IOException:参数不正确

C# 服务无法启动。System.IO.IOException:参数不正确,c#,registry,C#,Registry,我想通过注册表创建对服务的依赖关系,在on start方法中,我调用了一个方法,我可以在注册表中创建依赖关系。下面是代码 但是我在使用它时遇到了一些异常,请有人帮助我解决这个问题 protected override void On Start(string[] args) { //System.Diagnostics.Debugger.Launch(); serverDependency(); int.TryParse(Syst

我想通过注册表创建对服务的依赖关系,在on start方法中,我调用了一个方法,我可以在注册表中创建依赖关系。下面是代码

但是我在使用它时遇到了一些异常,请有人帮助我解决这个问题

    protected override void On Start(string[] args)
    {
        //System.Diagnostics.Debugger.Launch();
        serverDependency();

        int.TryParse(System.Configuration.ConfigurationSettings.AppSettings["IntervalTime"].ToString(), out intervalTime);

        // Dynamically create new timer
        System.Timers.Timer timScheduledTask = new System.Timers.Timer();

        // Timer interval is set in miliseconds,
        // In this case, we'll run a task every minute
        timScheduledTask.Interval = (intervalTime) * 1000;

        timScheduledTask.Enabled = true;

        // Add handler for Elapsed event
        timScheduledTask.Elapsed +=
        new System.Timers.ElapsedEventHandler(timScheduledTask_Elapsed);
    }

    public bool serverDependency()
    {
        string ServiceName = "MySQL";
       // BinaryFormatter bm = new BinaryFormatter();
        MemoryStream mem = new MemoryStream();
        Microsoft.Win32.RegistryKey reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("POSAgent");
        if (reg != null)
        {
            Microsoft.Win32.Registry.LocalMachine.DeleteSubKey("POSAgent");
        }
        reg = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("services");
        if (reg != null)
        {
            reg = reg.OpenSubKey("POSAgent");
            reg.SetValue("DependOnService", ServiceName, RegistryValueKind.MultiString);
        }
        else
        {
            reg = Microsoft.Win32.Registry.LocalMachine.CreateSubKey("POSAgent");
            reg.SetValue("DependOnService", ServiceName, RegistryValueKind.MultiString);
        }
        return true;
    }
以下是例外

服务无法启动。系统。木卫一。IO异常:参数 这是不正确的

在微软。赢32。注册表项。Win 32错误(Int 32错误 代码,字符串str)在Micro soft。赢32。注册表项。创建 子项(字符串子项、注册表项权限检查权限 在Microsoft.Win 32上检查注册表安全性(注册表安全性)。 注册表项。在POS代理服务中创建子密钥(字符串子密钥)。 POS代理。POS代理服务的服务器依赖项()。POS代理。在…上 在System.Service进程上启动(字符串[]args)。服务基地。 服务排队主回叫(对象状态)


IOException是CreateSubKey的一个记录在案的异常:“嵌套级别超过510-或-发生系统错误,例如删除密钥,或尝试在LocalMachine根目录中创建密钥。”后者正是您正在执行的操作。在软件子项内创建特定于应用程序的键。请注意“服务”也是错误的,该键存在于HKLM\System\CurrentControlSet中。这些密钥应该由服务安装程序编写,而不是由OnStart编写。IOException是CreateSubKey的一个记录在案的异常:“嵌套级别超过510-或者-发生系统错误,例如删除密钥,或者尝试在LocalMachine根目录中创建密钥。”后者正是您正在执行的操作。在软件子项内创建特定于应用程序的键。请注意“服务”也是错误的,该键存在于HKLM\System\CurrentControlSet中。这些密钥应该由服务安装程序编写,而不是由OnStart编写。