using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;
using System.Text;
namespace License_Service
{
public class SsWinService
{
public static bool ServiceExisted(string serviceName)
{
var services = ServiceController.GetServices();
foreach (var item in services)
{
if (item.ServiceName == serviceName)
return true;
}
return false;
}
public static void InstallService(string serviceName, string filepath, IDictionary stateSaver)
{
try
{
if (!ServiceExisted(serviceName))
{
//Install Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true; myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Install(stateSaver);
myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("installServiceError\r\n" + serviceName + "\r\n" + ex.Message);
}
}
public static void UnInstallService(string serviceName, string filepath)
{
try
{
if (ServiceExisted(serviceName))
{
//UnInstall Service
AssemblyInstaller myAssemblyInstaller = new AssemblyInstaller();
myAssemblyInstaller.UseNewContext = true;
myAssemblyInstaller.Path = filepath;
myAssemblyInstaller.Uninstall(null);
myAssemblyInstaller.Dispose();
}
}
catch (Exception ex)
{
throw new Exception("unInstallServiceError\r\n" + ex.Message);
}
}
public static void StartService(string serviceName)
{
if (ServiceExisted(serviceName))
{
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 == ServiceControllerStatus.Running)
{
break;
}
if (i == 59)
{
throw new Exception(serviceName + " 服务启动超时,请检查日志文件.");
}
}
}
}
else
{
throw new Exception(serviceName + " 服务不存在.");
}
}
public static void StopService(string serviceName)
{
if (ServiceExisted(serviceName))
{
ServiceController service = new ServiceController(serviceName);
if (service.Status == ServiceControllerStatus.Running)
{
service.Stop();
for (int i = 0; i < 60; i++)
{
service.Refresh();
System.Threading.Thread.Sleep(1000);
if (service.Status == ServiceControllerStatus.Stopped)
{
break;
}
if (i == 59)
{
throw new Exception(serviceName + "服务停止失败.");
}
}
}
}
}
}
}
private void btnStartAuto_Click(object sender, EventArgs e)
{
if (ServiceExisted("NIMS_PDM_Service"))
{
try
{
RegistryKey regist = Registry.LocalMachine;
RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
RegistryKey services = currentControlSet.OpenSubKey("Services");
RegistryKey servicesName = services.OpenSubKey("NIMS_PDM_Service", true);
servicesName.SetValue("Start", 2);
GetServerInfo();
}
catch (Exception ex)
{
throw new Exception("设置为自动启动发生错误" + ex.Message);
}
}
else
{
MessageBox.Show("NIMS_PDM_Service 服务不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void btnStartHand_Click(object sender, EventArgs e)
{
if (ServiceExisted("NIMS_PDM_Service"))
{
try
{
RegistryKey regist = Registry.LocalMachine;
RegistryKey sysReg = regist.OpenSubKey("SYSTEM");
RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
RegistryKey services = currentControlSet.OpenSubKey("Services");
RegistryKey servicesName = services.OpenSubKey("NIMS_PDM_Service", true);
servicesName.SetValue("Start", 3);
GetServerInfo();
}
catch (Exception ex)
{
throw new Exception("设置为手动启动发生错误" + ex.Message);
}
}
else
{
MessageBox.Show("NIMS_PDM_Service 服务不存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private bool ServiceExisted(string serviceName)
{
var services = ServiceController.GetServices();
foreach (var item in services)
{
if (item.ServiceName == serviceName)
return true;
}
return false;
}
浙公网安备 33010602011771号