對Windows Service(服務)的啟動類型進行操作的幾種方式

來源:互聯網
上載者:User

1、使用WMI管理主控台,對服務進行最直觀的控制
這當然不是我們需要的。

2、操縱註冊表中的相關配置資訊

1. Start Registry Editor (Regedt32.exe).
2. From the Registry menu, click Select Computer. Type in the name of the computer that is not responding, and then click OK.
3. Locate the following registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services \EventLog
4. Edit the following entries:

Value Name: Start
Data Type: REG_DWORD
Data: 3 (Default: 2)

(Data values are 0 = Boot, 1 = System, 2 = Automatic, 3 = Manual, 4 = Disabled)

註冊表操作的內容摘自http://support.microsoft.com/?kbid=158995
知道了修改註冊表中的哪個索引值,使用.NET編程操作就非常方便了。

3、WMI編程:使用VBScript

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='CiSvc'")
For Each objService in colRunningServices 
Wscript.Echo objService.DisplayName  & VbTab & objService.State
errReturnCode = objService.Change( , , , , "Automatic")  
objService.StartService()
Wscript.Echo objService.DisplayName  & VbTab & objService.State
Next

以上代碼從Microsoft TechNet指令碼中心得到。

4、WMI編程:使用.NET
其實前面的VBScript編程就是對WMI進行的,使用.NET編程同樣也可以對WM進行編程
相關資料在:Windows Management Instrumentation (WMI),
樣本文章:
Monitoring Application Health with WMI and .NET
WMI Scripting Primer: Part 1
WMI Scripting Primer: Part 2

5、編程:P/Invoke和.NET程式

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.