private void InstallService(IDictionary stateSaver,
string filepath) {
try { System.ServiceProcess.ServiceController service =
new System.ServiceProcess.ServiceController(
"ServiceName");
if(!ServiceIsExisted(
"ServiceName")) { AssemblyInstaller myAssemblyInstaller =
new AssemblyInstaller(); myAssemblyInstaller.UseNewContext =
true; myAssemblyInstaller.Path =filepath; myAssemblyInstaller.Install(stateSaver); myAssemblyInstaller.Commit(stateSaver); myAssemblyInstaller.Dispose(); service.Start(); }
else {
if (service.Status != System.ServiceProcess.ServiceControllerStatus.Running && service.Status != System.ServiceProcess.ServiceControllerStatus.StartPending) { service.Start(); } } }
catch (Exception ex) {
throw new Exception(
"installServiceError/n" + ex.Message); } }
private void UnInstallService(
string filepath) {
try {
if (ServiceIsExisted(
"ServiceName")) { AssemblyInstaller myAssemblyInstaller =
new AssemblyInstaller(); myAssemblyInstaller.UseNewContext =
true; myAssemblyInstaller.Path = filepath; myAssemblyInstaller.Uninstall(
null); myAssemblyInstaller.Dispose(); } }
catch (Exception ex) {
throw new Exception(
"unInstallServiceError/n" + ex.Message); } }
private bool ServiceIsExisted(
string serviceName) { ServiceController[] services = ServiceController.GetServices();
foreach (ServiceController s
in services) {
if (s.ServiceName == serviceName) {
return true; } }
return false; }
private void StartService(
string serviceName) {
if (ServiceIsExisted(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 == System.ServiceProcess.ServiceControllerStatus.Running) {
break; }
if (i == 59) {
throw new Exception(startServiceError.Replace(
"$s$", serviceName)); } } } } }
private void StopService(
string serviceName) {
if (ServiceIsExisted(serviceName)) { 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) {
throw new Exception(stopServiceError.Replace(
"$s$", serviceName)); } } } } }
public string StartService(
string serviceName) {
string strRst =
null; ManagementObject mo =
this.managementClass.CreateInstance(); mo.Path =
new ManagementPath(
this.strPath+
".Name=/""+serviceName+
"/"");
try {
if((
string)mo[
"State"]==
"Stopped") mo.InvokeMethod(
"StartService",
null); }
catch(ManagementException e) { strRst =e.Message; }
return strRst; }
public string PauseService(
string serviceName) {
string strRst =
null; ManagementObject mo =
this.managementClass.CreateInstance(); mo.Path =
new ManagementPath(
this.strPath+
".Name=/""+serviceName+
"/"");
try {
if((
bool)mo[
"acceptPause"]&&(
string)mo[
"State"]==
"Running") mo.InvokeMethod(
"PauseService",
null); }
catch(ManagementException e) { strRst =e.Message; }
return strRst; }
public string ResumeService(
string serviceName) {
string strRst =
null; ManagementObject mo =
this.managementClass.CreateInstance(); mo.Path =
new ManagementPath(
this.strPath+
".Name=/""+serviceName+
"/"");
try {
if((
bool)mo[
"acceptPause"]&&(
string)mo[
"State"]==
"Paused") mo.InvokeMethod(
"ResumeService",
null); }
catch(ManagementException e) { strRst =e.Message; }
return strRst; }
public string StopService(
string serviceName) {
string strRst =
null; ManagementObject mo =
this.managementClass.CreateInstance(); mo.Path =
new ManagementPath(
this.strPath+
".Name=/""+serviceName+
"/"");
try {
if((
bool)mo[
"AcceptStop"]) mo.InvokeMethod(
"StopService",
null); }
catch(ManagementException e) { strRst =e.Message; }
return strRst; } } }