Use C # code to start the service

Today we start the process to implement the use of the code or close the windows service


Processing windows startup and shutdown services in fact we need to focus on two things.

The first is to set the startup type of service - manual, automatic, Automatic (Delayed Start), disabled, and the second is to control the state of the service - start, stop, pause, resume.

After all, if the service is disabled, you can not start it

Here Insert Picture Description

OK, we start processing startup type.

Startup type is actually a key corresponding to the service registry

He stored 计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\服务名\Startthe

He has a value 1-4, respectively automatically (delayed start), automatic, manual, disabled. When the drop-down list and the picture above we see the same kind of

Here Insert Picture Description

Since it is the registry, then we can be controlled by the registry to read and write

	     protected string ReadRegistryValue(string service)
        {
            RegistryUtility.GetRegVal(RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Default),
                @"SYSTEM\CurrentControlSet\services\" + service, "Start", out var value);
            return value;
        }
        
        
	public static class RegistryUtility
    {
        public static bool GetRegVal(RegistryKey rootkey, string keypath, string keyname, out string rtn)
        {
            rtn = "";
            try
            {
                RegistryKey key = rootkey.OpenSubKey(keypath);
                rtn = key.GetValue(keyname).ToString();
                key.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

Note: If you use the code above to deal with TrustedInstallerthe service, you will find that if you try to modify the registry, not the rights issue will appear (even if you use administrator privileges).

At this point proposal directly using bat scripts"sc config {serviceName} start= {startValue}

OK, now it is the second issue, the state control services

C # is System.ServiceProcess.dllfor us an ServiceControllerobject that can be easily controlled by the state of the service it

Here Insert Picture Description

Here Insert Picture Description

Reference links:


This article will be updated frequently, please read personal blog Original: https://xinyuehtx.github.io/ , in order to avoid misleading the old error of knowledge, and a better reading experience.

Creative Commons License This work is Creative Commons Attribution - NonCommercial - ShareAlike 4.0 International License Agreement for licensing. Welcome to reprint, use, repost, but be sure to keep the article signed by Huang Tengxiao (containing links: https://xinyuehtx.github.io/ ), shall not be used for commercial purposes, be sure to publish the same work based on the paper license modification. If you have any questions, please contact me .

Published 77 original articles · won praise 1 · views 3189

Guess you like

Origin blog.csdn.net/htxhtx123/article/details/104387538