本文最后更新于 2024-05-07,文章可能存在过时内容,如有过时内容欢迎留言或者联系我进行反馈。
代码示例 {#%E4%BB%A3%E7%A0%81%E7%A4%BA%E4%BE%8B}
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,
/// &lt;summary&gt;服务未运行&lt;/summary&gt;
服务未运行 = 1,
/// &lt;summary&gt;服务正在启动&lt;/summary&gt;
服务正在启动 = 2,
/// &lt;summary&gt;服务正在停止&lt;/summary&gt;
服务正在停止 = 3,
/// &lt;summary&gt;服务正在运行&lt;/summary&gt;
服务正在运行 = 4,
/// &lt;summary&gt;服务即将继续&lt;/summary&gt;
服务即将继续 = 5,
/// &lt;summary&gt;服务即将暂停&lt;/summary&gt;
服务即将暂停 = 6,
/// &lt;summary&gt;服务已暂停&lt;/summary&gt;
服务已暂停 = 7
}
/// &lt;summary&gt;Windows服务操作帮助类&lt;/summary&gt;
public class ServiceUtil
{
/// &lt;summary&gt;检查服务存在的存在性&lt;/summary&gt;
/// &lt;param name=" NameService "&gt;服务名&lt;/param&gt;
/// &lt;returns&gt;存在返回 true,否则返回 false;&lt;/returns&gt;
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;
}
/// &lt;summary&gt;安装Windows服务&lt;/summary&gt;
/// &lt;param name="stateSaver"&gt;集合&lt;/param&gt;
/// &lt;param name="filepath"&gt;程序文件路径&lt;/param&gt;
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();
}
/// &lt;summary&gt;卸载Windows服务&lt;/summary&gt;
/// &lt;param name="filepath"&gt;程序文件路径&lt;/param&gt;
public static void UnInstallmyService(string filepath)
{
AssemblyInstaller AssemblyInstaller1 = new AssemblyInstaller();
AssemblyInstaller1.UseNewContext = true;
AssemblyInstaller1.Path = filepath;
AssemblyInstaller1.Uninstall(null);
AssemblyInstaller1.Dispose();
}
/// &lt;summary&gt;启动服务&lt;/summary&gt;
/// &lt;param name=" NameService "&gt;服务名&lt;/param&gt;
/// &lt;returns&gt;存在返回 true,否则返回 false;&lt;/returns&gt;
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;
}
/// &lt;summary&gt;停止服务&lt;/summary&gt;
/// &lt;param name=" NameService "&gt;服务名&lt;/param&gt;
/// &lt;returns&gt;存在返回 true,否则返回 false;&lt;/returns&gt;
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;
}
/// &lt;summary&gt;获取服务状态&lt;/summary&gt;
/// &lt;param name=" NameService "&gt;服务名&lt;/param&gt;
/// &lt;returns&gt;返回服务状态&lt;/returns&gt;
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;
}
/// &lt;summary&gt;获取服务安装路径&lt;/summary&gt;
/// &lt;param name="ServiceName"&gt;&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
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;
}
/// &lt;summary&gt;获取指定服务的版本号&lt;/summary&gt;
/// &lt;param name="serviceName"&gt;服务名称&lt;/param&gt;
/// &lt;returns&gt;&lt;/returns&gt;
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;
}
}
}
}