【CSharp】Windows服务操作帮助类
本文最后更新于 2024-05-07,文章可能存在过时内容,如有过时内容欢迎留言或者联系我进行反馈。
代码示例
using System;
using System.IO;
using Microsoft.Win32;
using System.Reflection;
using System.Collections;
using System.ServiceProcess;
using System.Configuration.Install;
namespace Common
{
/// <summary>服务状态</summary>
public enum ServiceStatus
{
/// <summary>服务未安装</summary>
服务未安装 = 0,
/// <summary>服务未运行</summary>
服务未运行 = 1,
/// <summary>服务正在启动</summary>
服务正在启动 = 2,
/// <summary>服务正在停止</summary>
服务正在停止 = 3,
/// <summary>服务正在运行</summary>
服务正在运行 = 4,
/// <summary>服务即将继续</summary>
服务即将继续 = 5,
/// <summary>服务即将暂停</summary>
服务即将暂停 = 6,
/// <summary>服务已暂停</summary>
服务已暂停 = 7
}
/// <summary>Windows服务操作帮助类</summary>
public class ServiceUtil
{
/// <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;
}
/// <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();
}
/// <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();
}
/// <summary>启动服务</summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool RunService(string NameService)
{
bool bo = true;
try
{
ServiceController sc = new ServiceController(NameService);
if (sc.Status.Equals(ServiceControllerStatus.Stopped) || sc.Status.Equals(ServiceControllerStatus.StopPending))
{
sc.Start();
}
}
catch (Exception ex)
{
bo = false;
}
return bo;
}
/// <summary>停止服务</summary>
/// <param name=" NameService ">服务名</param>
/// <returns>存在返回 true,否则返回 false;</returns>
public static bool StopService(string NameService)
{
bool bo = true;
try
{
ServiceController sc = new ServiceController(NameService);
if (!sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();
}
}
catch (Exception ex)
{
bo = false;
}
return bo;
}
/// <summary>获取服务状态</summary>
/// <param name=" NameService ">服务名</param>
/// <returns>返回服务状态</returns>
public static ServiceStatus GetServiceStatus(string NameService)
{
ServiceStatus status = 0;
try
{
ServiceController sc = new ServiceController(NameService);
status = (ServiceStatus)Convert.ToInt16(sc.Status);
}
catch (Exception ex)
{
status = 0;
}
return status;
}
/// <summary>获取服务安装路径</summary>
/// <param name="ServiceName"></param>
/// <returns></returns>
public static string GetWindowsServiceInstallPath(string ServiceName)
{
string path = "";
try
{
string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;
path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();
path = path.Replace("\"", string.Empty);//替换掉双引号
FileInfo fi = new FileInfo(path);
path = fi.Directory.ToString();
}
catch (Exception ex)
{
path = "";
}
return path;
}
/// <summary>获取指定服务的版本号</summary>
/// <param name="serviceName">服务名称</param>
/// <returns></returns>
public static string GetServiceVersion(string serviceName)
{
if (string.IsNullOrEmpty(serviceName))
{
return string.Empty;
}
try
{
string path = GetWindowsServiceInstallPath(serviceName) + "\\" + serviceName + ".exe";
Assembly assembly = Assembly.LoadFile(path);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;
return version.ToString();
}
catch (Exception ex)
{
return string.Empty;
}
}
}
}
- 感谢你赐予我前进的力量
赞赏者名单
因为你们的支持让我意识到写文章的价值🙏
评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果