none
Change Startup Type of Windows Service RRS feed

  • Question

  • Hi,

    I was wondering if anyone knows if it possible to change the startup type of a service programatically, either within the service itself or using a service conrtoller?

    Thanks

    Ryan
    Friday, August 12, 2005 2:15 AM

Answers

  • From Service Controller:
    Control Panel....Computer Management....Services
    Friday, August 12, 2005 3:28 PM
    Moderator
  • Hi Ryan,

    Service startup type is set during installation (see
    ServiceInstaller.StartType).There is currently no managed way to
    change this after installation.

    However you can change it programmatically in code:


    using System;
    using Microsoft.Win32;

    public enum ServiceStart
    {
    Boot = 0,
    System = 1,
    Automatic = 2,
    Manual = 3,
    Disabled = 4
    }

    public class ServiceController2 : System.ServiceProcess.ServiceController
    {

    public ServiceStart ServiceStart
    {
     get
     {
        RegistryKey key = Registry.LocalMachine.OpenSubKey(
         "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName );
          ServiceStart start = (ServiceStart)key.GetValue("Start");
         key.Close();
         return (start);
     }
     
     set
     {
           RegistryKey key = Registry.LocalMachine.OpenSubKey(
             "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName, true );
           key.SetValue( "Start", (int)value );
           key.Close();
    }
    }
    }
    }


     


    Regards,
    Vikram

    Friday, August 12, 2005 5:54 PM
    Moderator

All replies

  • I believe this is a registry entry. I would go through the registryusing regedit searching for string names of specific services in services.msc
    Friday, August 12, 2005 3:24 PM
    Moderator
  • From Service Controller:
    Control Panel....Computer Management....Services
    Friday, August 12, 2005 3:28 PM
    Moderator
  • Hi Ryan,

    Service startup type is set during installation (see
    ServiceInstaller.StartType).There is currently no managed way to
    change this after installation.

    However you can change it programmatically in code:


    using System;
    using Microsoft.Win32;

    public enum ServiceStart
    {
    Boot = 0,
    System = 1,
    Automatic = 2,
    Manual = 3,
    Disabled = 4
    }

    public class ServiceController2 : System.ServiceProcess.ServiceController
    {

    public ServiceStart ServiceStart
    {
     get
     {
        RegistryKey key = Registry.LocalMachine.OpenSubKey(
         "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName );
          ServiceStart start = (ServiceStart)key.GetValue("Start");
         key.Close();
         return (start);
     }
     
     set
     {
           RegistryKey key = Registry.LocalMachine.OpenSubKey(
             "SYSTEM\\CurrentControlSet\\Services\\" + this.ServiceName, true );
           key.SetValue( "Start", (int)value );
           key.Close();
    }
    }
    }
    }


     


    Regards,
    Vikram

    Friday, August 12, 2005 5:54 PM
    Moderator