1. 示例源碼: "WindowsServiceSample" 2. ServiceHelper源碼: "ServiceHelper" 1. 創建Windows Service項目,如圖: 2. 配置服務參數 3. 安裝,啟動,停止,卸載服務 實現代碼: ...
- 示例源碼:WindowsServiceSample
- ServiceHelper源碼:ServiceHelper
1. 創建Windows Service項目,如圖:
2. 配置服務參數
3. 安裝,啟動,停止,卸載服務
實現代碼:
private string ServicePath => txtServicePath.Text.Trim();
private string ServiceName => "ServiceSample";
private void BtnStart_Click(object sender, EventArgs e)
{
if (!ServiceHelper.IsExisted(ServiceName))
{
MessageBoxHelper.ShowError($"{ServiceName}不存在");
return;
}
ServiceHelper.Start(ServiceName);
}
private void BtnStop_Click(object sender, EventArgs e)
{
if (!ServiceHelper.IsExisted(ServiceName))
{
MessageBoxHelper.ShowError($"{ServiceName}不存在");
return;
}
ServiceHelper.Stop(ServiceName);
}
private void BtnInstall_Click(object sender, EventArgs e)
{
if (ServiceHelper.IsExisted(ServiceName))
{
MessageBoxHelper.ShowError($"{ServiceName}已經存在");
return;
}
ServiceHelper.Install(ServicePath);
}
private void BtnUnInstall_Click(object sender, EventArgs e)
{
if (!ServiceHelper.IsExisted(ServiceName))
{
MessageBoxHelper.ShowError($"{ServiceName}不存在");
return;
}
ServiceHelper.Uninstall(ServicePath);
}
}