如何利用c#修改services的Startup type

來源:互聯網
上載者:User

我們知道大部分的services的操作可以通過ServiceController來實現,包括services的開啟,停止,暫停,還有擷取service的status。但是這裡關於services的修改Startup type這點,貌似ServiceController不好做到,我們可以這樣來做:

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();        }    }}

 

 

 

上面使用了ManagementPath類,或者你也可以這樣:

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();        }    }}

 

 

 

這段代碼使用的是ManagementObject類,裡面輸出的ReturnValue是一個標誌,如果值為0就是修改成功了。

 

這裡需要注意的一點:C#必須以管理員的許可權運行才能達到效果的,不然service的startmode修改是沒有效果的。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.