日期:2014-05-18  浏览次数:20780 次

怎样通过程序启动window服务
我现在做了一个windows服务,我还需要做一个管理此服务的程序,现在的问题是怎样通过程序启动和停止服务!

------解决方案--------------------
ServiceController sc = new ServiceController( "ServiceName ");
if (sc.Status == ServiceControllerStatus.Stopped)
{
sc.Start();
sc.Stop();
}

------解决方案--------------------
ServiceController sc = new ServiceController( "ServiceName ");
if (s == "START " && sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Start();
}
if (s == "STOP " && !sc.Status.Equals(ServiceControllerStatus.Stopped))
{
sc.Stop();