2025-03-28 09:49:56 +08:00

225 lines
7.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Win32;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace Transmission.SDK
{
public class WindowsServiceHelper
{
#region
/// <summary>
/// 安装服务
/// </summary>
private bool InstallService(string NameService)
{
bool flag = true;
if (!IsServiceIsExisted(NameService))
{
try
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe";
InstallmyService(null, serviceFileName);
}
catch
{
flag = false;
}
}
return flag;
}
#endregion
#region
/// <summary>
/// 卸载服务
/// </summary>
private bool UninstallService(string NameService)
{
bool flag = true;
if (IsServiceIsExisted(NameService))
{
try
{
string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
string serviceFileName = location.Substring(0, location.LastIndexOf('\\') + 1) + NameService + ".exe";
UnInstallmyService(serviceFileName);
}
catch
{
flag = false;
}
}
return flag;
}
#endregion
#region
/// <summary>
/// 检查服务是否已存在
/// </summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool IsServiceIsExisted(string NameService)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName.ToLower() == NameService.ToLower())
{
return true;
}
}
return false;
}
#endregion
#region Windows服务
/// <summary>
/// 安装Windows服务
/// </summary>
/// <param name="stateSaver">集合</param>
/// <param name="filepath">程序文件路径</param>
public static void InstallmyService(IDictionary stateSaver, string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Install(stateSaver);
AssemblyInstaller1.Commit(stateSaver);
AssemblyInstaller1.Dispose();
}
#endregion
#region Windows服务
/// <summary>
/// 卸载Windows服务
/// </summary>
/// <param name="filepath">程序文件路径</param>
public static void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Uninstall(null);
AssemblyInstaller1.Dispose();
}
#endregion
#region window服务是否启动
/// <summary>
/// 判断某个Windows服务是否启动
/// </summary>
/// <returns></returns>
public static bool IsServiceStart(string serviceName)
{
ServiceController psc = new ServiceController(serviceName);
bool bStartStatus = false;
try
{
if (!psc.Status.Equals(ServiceControllerStatus.Stopped))
{
bStartStatus = true;
}
return bStartStatus;
}
catch (Exception ex)
{
return bStartStatus;
}
}
#endregion
#region
/// <summary>
/// 修改服务的启动项
/// </summary>
/// <param name="startType">启动类型2为自动,3为手动 </param>
/// <param name="serviceName">服务名称</param>
/// <returns></returns>
public static bool ChangeServiceStartType(int startType, string serviceName)
{
try
{
RegistryKey regist = Registry.LocalMachine;
RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
RegistryKey services = currentControlSet.OpenSubKey("Services");
RegistryKey servicesName = services.OpenSubKey(serviceName, true);
servicesName.SetValue("Start", startType);
}
catch (Exception ex)
{
return false;
}
return true;
}
#endregion
#region
public static bool StartService(string serviceName)
{
bool flag = true;
if (IsServiceIsExisted(serviceName))
{
ServiceController service = new ServiceController(serviceName);
if (service.Status != ServiceControllerStatus.Running && service.Status != ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion
#region
public static bool StopService(string serviceName)
{
bool flag = true;
if (IsServiceIsExisted(serviceName))
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion
}
}