最近想做這樣一個(gè)效果,在網(wǎng)頁控制Window服務(wù)啟動(dòng)和停止和狀態(tài):
添加引用
- Window服務(wù)在程序集System.ServiceProcess纠永,然后增加using System.ServiceProcess;
- 服務(wù)有幾個(gè)狀態(tài),它是枚舉類型既绕,如下:
//
// 摘要:
// 服務(wù)未運(yùn)行速种。這對應(yīng)于 Win32 SERVICE_STOPPED 常數(shù),該常數(shù)定義為 0x00000001径缅。
Stopped = 1,
//
// 摘要:
// 服務(wù)正在啟動(dòng)。這對應(yīng)于 Win32 SERVICE_START_PENDING 常數(shù)烙肺,該常數(shù)定義為 0x00000002纳猪。
StartPending = 2,
//
// 摘要:
// 服務(wù)正在停止。這對應(yīng)于 Win32 SERVICE_STOP_PENDING 常數(shù)桃笙,該常數(shù)定義為 0x00000003氏堤。
StopPending = 3,
//
// 摘要:
// 服務(wù)正在運(yùn)行。這對應(yīng)于 Win32 SERVICE_RUNNING 常數(shù)搏明,該常數(shù)定義為 0x00000004鼠锈。
Running = 4,
//
// 摘要:
// 服務(wù)即將繼續(xù)。這對應(yīng)于 Win32 SERVICE_CONTINUE_PENDING 常數(shù)星著,該常數(shù)定義為 0x00000005购笆。
ContinuePending = 5,
//
// 摘要:
// 服務(wù)即將暫停。這對應(yīng)于 Win32 SERVICE_PAUSE_PENDING 常數(shù)虚循,該常數(shù)定義為 0x00000006同欠。
PausePending = 6,
//
// 摘要:
// 服務(wù)已暫停样傍。這對應(yīng)于 Win32 SERVICE_PAUSED 常數(shù),該常數(shù)定義為 0x00000007铺遂。
Paused = 7
更新服務(wù)類
- 代碼如下
public static class WindowService
{
private const string SERVICE_NAME = "Fax"; //服務(wù)名稱
/// <summary>
/// 獲取Window服務(wù)狀態(tài)
/// </summary>
/// <returns></returns>
public static ServiceControllerStatus GetWinServiceState()
{
ServiceController sc = new ServiceController(SERVICE_NAME);
return sc.Status;
}
/// <summary>
/// 停止Window服務(wù)
/// </summary>
public static void StopWinService()
{
ServiceController sc = new ServiceController(SERVICE_NAME);
sc.Stop();
}
/// <summary>
/// 啟動(dòng)Window服務(wù)
/// </summary>
public static void StartWinService()
{
ServiceController sc = new ServiceController(SERVICE_NAME);
sc.Start();
}
/// <summary>
/// 啟動(dòng)Window服務(wù)
/// </summary>
public static void RestoreWinService()
{
ServiceController sc = new ServiceController(SERVICE_NAME);
sc.Refresh();
}
}
服務(wù)權(quán)限問題
-
部署可能會(huì)遇到權(quán)限問題衫哥,如下:
-
那么在可以修改應(yīng)用程序池的標(biāo)識(shí),如下
- 以上為個(gè)人遇到問題襟锐,僅供參考撤逢!