http://www.cnblogs.com/yanweidie/archive/2014/02/10/3542670.html
public static void CheckServerState(string ServiceName,string ServicePath){
ServiceController[] service = ServiceController.GetServices();
bool isStart = false;
bool isExite = false;
ServiceController server=null;
for (int i = 0; i < service.Length; i++)
{
if (service[i].ServiceName.ToUpper().Equals(ServiceName.ToUpper()))
{
isExite = true;
server = service[i];
if (service[i].Status == ServiceControllerStatus.Running)
{
isStart = true;
break;
}
}
}
if (!isExite)
{
try
{
string[] cmdline = { };
TransactedInstaller transactedInstaller = new TransactedInstaller();
AssemblyInstaller assemblyInstaller = new AssemblyInstaller(ServicePath, cmdline);
transactedInstaller.Installers.Add(assemblyInstaller);
transactedInstaller.Install(new System.Collections.Hashtable());
server.Start();
}
catch (Exception ex)
{
}
}
else
{
if (!isStart)
{
try
{
server.Start();
}
catch (Exception ex)
{ }
}
}
}