Esempio n. 1
0
        /// <summary>
        /// 获取服务启动类型
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        public static WinServiceLaunchType GetLaunchType(string serviceName)
        {
            if (!IsExist(serviceName))
            {
                return(WinServiceLaunchType.NONE);
            }

            RegistryKey key      = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\services\" + serviceName);
            object      startObj = key.GetValue("Start");

            key.Close();
            if (null == startObj)
            {
                return(WinServiceLaunchType.NONE);
            }

            WinServiceLaunchType winServiceLaunchType = (WinServiceLaunchType)Enum.Parse(typeof(WinServiceLaunchType), startObj.ToString());

            return(winServiceLaunchType);
        }
Esempio n. 2
0
        /// <summary>
        /// 设置服务启动类型
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <param name="winServiceLaunchMode">启动类型</param>
        public static void SetLaunchType(string serviceName, WinServiceLaunchType winServiceLaunchType)
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\ControlSet001\services\" + serviceName, true);

            if (winServiceLaunchType == WinServiceLaunchType.NONE)
            {
                return;
            }
            else if (winServiceLaunchType == WinServiceLaunchType.AUTOMATIC_DELAY)
            {
                key.SetValue("Start", 2);
                string[] valueNames       = key.GetValueNames();
                string   DelayedAutoStart = "DelayedAutoStart";
                key.SetValue(DelayedAutoStart, 1);
            }
            else
            {
                key.SetValue("Start", (int)winServiceLaunchType);
            }
            key.Close();
        }