How can c be used to modify Startup type of services

  • 2020-05-17 06:12:56
  • OfStack

We know that most services operations can be performed by ServiceController, including the start, stop, pause of services, and the status of service. But this modification of services, Startup type, seems difficult for ServiceController. We can do it like this:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ServicesStartup
{
    class Program
    {
        public enum StartupType
        {
            Automatic,
            Disabled,
            Manual
        }
        public static void SetStartupType(string serviceName, StartupType startupType)
        {
            string type = startupType.ToString();
            try
            {
                ManagementPath mp = new ManagementPath(string.Format("Win32_Service.Name='{0}'", serviceName));
                if (mp != null)
                {
                    using (ManagementObject mo = new ManagementObject(mp))
                    {
                        object[] parameters = new object[1] { type };
                        mo.InvokeMethod("ChangeStartMode", parameters);                        
                    }
                }
            }
            catch (ManagementException ex)
            {
                Console.WriteLine("An error occured while trying to searching the WMI method: " + ex.ToString());
            }

        }
        static void Main(string[] args)
        {
            SetStartupType("gupdate", StartupType.Automatic);  
            Console.ReadKey();
        }
    }
}

The ManagementPath class is used above, or you can:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace ServicesStartup
{
    class Program
    {
       static void Main(string[] args)
        {
            try
            {
                ManagementObject classInstance = new ManagementObject("root\\CIMV2",
                    "Win32_Service.Name='gupdate'", null);
                // Obtain in-parameters for the method.
                ManagementBaseObject inParams = classInstance.GetMethodParameters("ChangeStartMode");
                // Add the input parameters.
                inParams["StartMode"] = "Automatic";                
                // Execute the method and obtain the return values.
                ManagementBaseObject outParams = classInstance.InvokeMethod("ChangeStartMode", inParams, null);
                // List outParams
                Console.WriteLine("Out parameters:");
                Console.WriteLine("ReturnValue: " + outParams["ReturnValue"]);
            }
            catch (ManagementException err)
            {
                Console.WriteLine("An error occured while trying to execute the WMI emthod: " + err.ToString());
            }
            Console.ReadKey();
        }
    }
}

This code USES the ManagementObject class, in which the output ReturnValue is a flag. If the value is 0, the modification is successful.

One point to note here :C# must be run with administrator privileges to be effective, otherwise the startmode modification of service will not be effective.


Related articles: