C#啟動windows服務方法的相關問題分析
更新時間:2012年12月16日 14:47:26 作者:
C#啟動windows服務的方法都是什么呢?C#啟動服務類型為Disabled的windows服務會遇到什么樣的問題呢?那么本文就向你介紹C#啟動windows服務的方法的相關內(nèi)容
C#啟動windows服務的方法都是什么呢?C#啟動服務類型為Disabled的windows服務會遇到什么樣的問題呢?那么本文就向你介紹C#啟動windows服務的方法的相關內(nèi)容。
C#啟動windows服務的方法是什么呢?來讓我們開始吧:
C#啟動windows服務的由來:我們知道, 在C#代碼中啟動一個已經(jīng)存在的windows服務,我們可以用這樣的代碼來完成:
//ACPI is an example of service name
System.ServiceProcess.ServiceController
service = new ServiceController("ACPI");
service.Start();
C#啟動windows服務遇到的問題, 如果服務類型是Disabled, 那么start方法就會引發(fā)異常。 一般的做法是先修改服務的啟動類型, 然后啟動該服務:
using Microsoft.Win32;
string keyPath = @"SYSTEM\CurrentControlSet\Services\ACPI";
RegistryKey key = Registry.LocalMachine.OpenSubKey(keyPath, true);
int val = -1;
bool bConverted = Int32.TryParse(key.GetValue("Start").
ToString(), out val); if(bConverted){
if ( val == 4){key.SetValue("Start", 3);
}
}
System.ServiceProcess.ServiceController
service = new ServiceController("ACPI");
service.Start();
總結(jié)一下修改服務的啟動方式有兩種方法:
C#啟動windows服務1. 修改注冊表
windows 服務的注冊表地址為 :
[\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ServiceName]
其中子鍵Start代表了啟動類型. 類如"Start"=dword:00000002
其中2為Automatic, 3為Manul, 4為Disabled
C#啟動windows服務2.用API
BOOL ChangeServiceConfig(
SC_HANDLE hService,
DWORD dwServiceType,
DWORD dwStartType,
DWORD dwErrorControl,
LPCTSTR lpBinaryPathName,
LPCTSTR lpLoadOrderGroup,
LPDWORD lpdwTagId,
LPCTSTR lpDependencies,
LPCTSTR lpServiceStartName,
LPCTSTR lpPassword,
LPCTSTR lpDisplayName
);
C#啟動windows服務的方法淺析就向你介紹到這里,希望對你學習和了解C#啟動windows服務有所幫助。
C#啟動windows服務的方法是什么呢?來讓我們開始吧:
C#啟動windows服務的由來:我們知道, 在C#代碼中啟動一個已經(jīng)存在的windows服務,我們可以用這樣的代碼來完成:
復制代碼 代碼如下:
//ACPI is an example of service name
System.ServiceProcess.ServiceController
service = new ServiceController("ACPI");
service.Start();
C#啟動windows服務遇到的問題, 如果服務類型是Disabled, 那么start方法就會引發(fā)異常。 一般的做法是先修改服務的啟動類型, 然后啟動該服務:
復制代碼 代碼如下:
using Microsoft.Win32;
string keyPath = @"SYSTEM\CurrentControlSet\Services\ACPI";
RegistryKey key = Registry.LocalMachine.OpenSubKey(keyPath, true);
int val = -1;
bool bConverted = Int32.TryParse(key.GetValue("Start").
ToString(), out val); if(bConverted){
if ( val == 4){key.SetValue("Start", 3);
}
}
System.ServiceProcess.ServiceController
service = new ServiceController("ACPI");
service.Start();
總結(jié)一下修改服務的啟動方式有兩種方法:
C#啟動windows服務1. 修改注冊表
windows 服務的注冊表地址為 :
[\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\ServiceName]
其中子鍵Start代表了啟動類型. 類如"Start"=dword:00000002
其中2為Automatic, 3為Manul, 4為Disabled
C#啟動windows服務2.用API
復制代碼 代碼如下:
BOOL ChangeServiceConfig(
SC_HANDLE hService,
DWORD dwServiceType,
DWORD dwStartType,
DWORD dwErrorControl,
LPCTSTR lpBinaryPathName,
LPCTSTR lpLoadOrderGroup,
LPDWORD lpdwTagId,
LPCTSTR lpDependencies,
LPCTSTR lpServiceStartName,
LPCTSTR lpPassword,
LPCTSTR lpDisplayName
);
C#啟動windows服務的方法淺析就向你介紹到這里,希望對你學習和了解C#啟動windows服務有所幫助。
相關文章
C#數(shù)據(jù)結(jié)構(gòu)與算法揭秘二
上文對數(shù)據(jù)結(jié)構(gòu)與算法,有了一個簡單的概述與介紹,這篇文章,我們介紹一中典型數(shù)據(jù)結(jié)構(gòu)——線性結(jié)構(gòu)2012-10-10
C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能
一直喜歡用Google Translate API進行在線翻譯,但是服務越來越慢這篇文章,所以只能換一個了,主要給大家介紹了關于C#調(diào)用百度翻譯API實現(xiàn)一個翻譯功能的相關資料,需要的朋友可以參考下2021-06-06
C#實現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法
這篇文章主要介紹了C#實現(xiàn)異步連接Sql Server數(shù)據(jù)庫的方法,涉及C#中await方法的相關使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
C#操作SQLite數(shù)據(jù)庫方法小結(jié)(創(chuàng)建,連接,插入,查詢,刪除等)
這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫方法,包括針對SQLite數(shù)據(jù)庫的創(chuàng)建,連接,插入,查詢,刪除等操作,并提供了一個SQLite的封裝類,需要的朋友可以參考下2016-07-07
C#實現(xiàn)字符串與圖片的Base64編碼轉(zhuǎn)換操作示例
這篇文章主要介紹了C#實現(xiàn)字符串與圖片的Base64編碼轉(zhuǎn)換操作,結(jié)合實例形式分析了C#針對base64編碼與圖片的相互轉(zhuǎn)換操作技巧,需要的朋友可以參考下2017-06-06

