C# 通过代码安装、卸载、启动、停止服务
清泛原创
直接贴代码,亲测可用:
using System.ServiceProcess;
using System.Configuration.Install;
#region Windows服务控制区
#region 安装服务
private void InstallService(string filepath, string serviceName)
{
try
{
IDictionary _SavedState = new Hashtable();
ServiceController service = new ServiceController(serviceName);
string dispName = string.Empty;
if (!ServiceIsExisted(serviceName, ref dispName))
{
// Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Install(_SavedState);
myAssemblyInstaller.Commit(_SavedState);
myAssemblyInstaller.Dispose();
// --Start Service
// service.Start();
}
else
{
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
}
}
statusMsg.Text = "服务安装成功!";
}
catch (Exception ex)
{
statusMsg.Text = "安装服务失败!" + ex.Message;
}
}
#endregion
#region 卸载windows服务
private void UnInstallService(string filepath, string serviceName)
{
try
{
string dispName = string.Empty;
if (ServiceIsExisted(serviceName, ref dispName))
{
// UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
statusMsg.Text = "服务卸载成功!";
}
catch (Exception ex)
{
statusMsg.Text = "卸载服务失败!" + ex.Message;
}
}
#endregion
#region 判断window服务是否存在
private bool ServiceIsExisted(string serviceName, ref string dispName)
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s in services)
{
if (s.ServiceName == serviceName)
{
dispName = s.DisplayName;
return true;
}
}
return false;
}
#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)
{
throw new Exception(ex.Message);
}
}
#endregion
#region 启动服务
private bool StartService(string serviceName)
{
bool flag = true;
string dispName = string.Empty;
if (ServiceIsExisted(serviceName, ref dispName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending)
{
service.Start();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion
#region 停止服务
private bool StopService(string serviceName)
{
bool flag = true;
string dispName = string.Empty;
if (ServiceIsExisted(serviceName, ref dispName))
{
System.ServiceProcess.ServiceController service = new System.ServiceProcess.ServiceController(serviceName);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == System.ServiceProcess.ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
flag = false;
}
}
}
}
return flag;
}
#endregion
#endregion
需要添加以下命名空间:using System.ServiceProcess;
using System.Configuration.Install;
上一篇:C# HashCode及Equals
下一篇:BinaryFormatter SoapFormatter XmlSerializer命名空间