怎么利用c#修改services的Startup type
我們知道大部分的services的操作可以通過ServiceController來實(shí)現(xiàn),包括services的開啟,停止,暫停,還有獲取service的status。但是這里關(guān)于services的修改Startup type這點(diǎn),貌似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是一個(gè)標(biāo)志,如果值為0就是修改成功了。
這里需要注意的一點(diǎn):C#必須以管理員的權(quán)限運(yùn)行才能達(dá)到效果的,不然service的startmode修改是沒有效果的。
相關(guān)文章
解析在內(nèi)部循環(huán)中Continue外部循環(huán)的使用詳解
本篇文章是對(duì)在內(nèi)部循環(huán)中Continue外部循環(huán)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C# 字符串string和內(nèi)存流MemoryStream及比特?cái)?shù)組byte[]之間相互轉(zhuǎn)換
本文主要介紹字符串string和內(nèi)存流MemoryStream及比特?cái)?shù)組byte[]之間相互轉(zhuǎn)換的方法,需要的小伙伴可以參考一下。2016-05-05
Avalonia封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類
這篇文章主要為大家詳細(xì)介紹了Avalonia如何封裝實(shí)現(xiàn)指定組件允許拖動(dòng)的工具類,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起來學(xué)習(xí)學(xué)習(xí)吧2023-03-03
C#窗體-數(shù)據(jù)庫(kù)連接及登錄功能的實(shí)現(xiàn)案例
這篇文章主要介紹了C#窗體-數(shù)據(jù)庫(kù)連接及登錄功能的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-12-12
C#窗體實(shí)現(xiàn)點(diǎn)餐系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#窗體實(shí)現(xiàn)點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
C#開發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件
這篇文章主要為大家詳細(xì)介紹了C#開發(fā)windows服務(wù)實(shí)現(xiàn)自動(dòng)從FTP服務(wù)器下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)單的計(jì)算器小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
c#實(shí)現(xiàn)幾種數(shù)據(jù)庫(kù)的大數(shù)據(jù)批量插入
這篇文章主要介紹了c#實(shí)現(xiàn)幾種數(shù)據(jù)庫(kù)的大數(shù)據(jù)批量插入,主要包括SqlServer、Oracle、SQLite和MySQL,有興趣的可以了解一下。2017-01-01
c#基礎(chǔ)之?dāng)?shù)組與接口使用示例(遍歷數(shù)組 二維數(shù)組)
本文主要介紹了c#基礎(chǔ)知識(shí)中的數(shù)組與接口使用方法,結(jié)合示例,大家一看就明白2014-01-01

