using System;
using System.Security.Principal;
using System.ServiceProcess;
using System.Windows.Forms;
namespace windowsServiceCTRL
{
public static class ServicesHelper
{
/// <summary>
/// 判断是否安装了某个服务
/// </summary>
/// <param name="serviceName"></param>
/// <returns></returns>
public static bool ISWindowsServiceInstalled(string serviceName)
{
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
{
return true;
}
}
return false;
}
catch
{
return false;
}
}
/// <summary>
/// 确定当前主体是否属于具有指定 Administrator 的 Windows 用户组
/// </summary>
/// <returns>如果当前主体是指定的 Administrator 用户组的成员,则为 true;否则为 false。</returns>
public static bool IsAdministrator()
{
bool result;
try
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
result = principal.IsInRole(WindowsBuiltInRole.Administrator);
//http://www.cnblogs.com/Interkey/p/RunAsAdmin.html
//AppDomain domain = Thread.GetDomain();
//domain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
//WindowsPrincipal windowsPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;
//result = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch
{
result = false;
}
return result;
}
/// <summary>
/// 启动某个服务,以管理员身份运行
/// </summary>
/// <param name="serviceName"></param>
public static bool StartService(string serviceName)
{
if(IsAdministrator() == false)
{
startAsAdmin();
return false;
}
bool ret = false;
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
break;
}
}
ret = true;
}
catch {
ret = false;
}
return ret;
}
private static void startAsAdmin()
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = Application.ExecutablePath;
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//退出
Environment.Exit(0);
}
/// <summary>
/// 启动某个服务,以管理员身份运行
/// </summary>
/// <param name="serviceName"></param>
public static bool StartServiceCMD(string serviceName)
{
try
{
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
proc.StandardInput.WriteLine("net start " + serviceName);
proc.WaitForExit(1000);
proc.Close();
}
return true;
}
catch (Exception ex)
{
MessageBox.Show("Start CMD Error:" + ex.Message);
return false;
}
}
/// <summary>
/// 停止某个服务
/// </summary>
/// <param name="serviceName"></param>
public static bool StopService(string serviceName)
{
bool ret = false;
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
break;
}
}
ret = true;
}
catch {
ret = false;
}
return ret;
}
/// <summary>
/// 判断某个服务是否启动
/// </summary>
/// <param name="serviceName"></param>
public static bool ISStart(string serviceName)
{
bool result = true;
try
{
ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController service in services)
{
if (service.ServiceName.Trim().ToUpper().Equals(serviceName.Trim().ToUpper()))
{
if ((service.Status == ServiceControllerStatus.Stopped)
|| (service.Status == ServiceControllerStatus.StopPending))
{
result = false;
break;
}
}
}
}
catch {
result = false;
}
return result;
}
}
}