116 lines
3.7 KiB
C#
116 lines
3.7 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Diagnostics;
|
|||
|
using System.Net;
|
|||
|
using System.Net.Sockets;
|
|||
|
using System.ServiceProcess;
|
|||
|
|
|||
|
namespace NetMonitorServer
|
|||
|
{
|
|||
|
class SetService : ActionClass
|
|||
|
{
|
|||
|
ServiceController serviceController;
|
|||
|
|
|||
|
private bool setServiceStart(Queue<string> data)
|
|||
|
{
|
|||
|
if (data.Count != 1)
|
|||
|
{
|
|||
|
this.sendMessage("To less arguments for SetService.setServiceStart class");
|
|||
|
return false;
|
|||
|
}
|
|||
|
this.serviceController.ServiceName = data.Dequeue();
|
|||
|
try
|
|||
|
{
|
|||
|
this.serviceController.Start();
|
|||
|
this.serviceController.WaitForStatus(ServiceControllerStatus.Running, new TimeSpan(0, 0, 30));
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
this.sendMessage(e.Message);
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (this.serviceController.Status == ServiceControllerStatus.Running)
|
|||
|
{
|
|||
|
this.sendMessage("true");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
this.sendMessage("The service isn't started");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private bool setServiceStop(Queue<string> data)
|
|||
|
{
|
|||
|
if (data.Count != 1)
|
|||
|
{
|
|||
|
this.sendMessage("To less arguments for SetService.setServiceStop class");
|
|||
|
return false;
|
|||
|
}
|
|||
|
this.serviceController.ServiceName = data.Dequeue();
|
|||
|
try
|
|||
|
{
|
|||
|
this.serviceController.Stop();
|
|||
|
this.serviceController.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 30));
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
this.sendMessage(e.Message);
|
|||
|
return false;
|
|||
|
}
|
|||
|
if (this.serviceController.Status == ServiceControllerStatus.Stopped)
|
|||
|
{
|
|||
|
this.sendMessage("true");
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
this.sendMessage("The service isn't stopped");
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
private bool getServiceStatus(Queue<string> data)
|
|||
|
{
|
|||
|
if (data.Count != 1)
|
|||
|
{
|
|||
|
this.sendMessage("To less arguments for SetService.setServiceStatus class");
|
|||
|
return false;
|
|||
|
}
|
|||
|
try
|
|||
|
{
|
|||
|
this.serviceController.ServiceName = data.Dequeue();
|
|||
|
if (this.serviceController.Status == ServiceControllerStatus.Running)
|
|||
|
this.sendMessage("running");
|
|||
|
else
|
|||
|
this.sendMessage("stopped");
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
this.sendMessage(e.Message);
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
internal override bool Run(Queue<string> arguments)
|
|||
|
{
|
|||
|
if (arguments.Count != 2)
|
|||
|
{
|
|||
|
this.sendMessage("To less arguments for SetService class");
|
|||
|
return false;
|
|||
|
}
|
|||
|
this.serviceController = new ServiceController();
|
|||
|
switch (arguments.Dequeue().ToLower())
|
|||
|
{
|
|||
|
case "start": return this.setServiceStart(arguments);
|
|||
|
case "stop": return this.setServiceStop(arguments);
|
|||
|
case "status": return this.getServiceStatus(arguments);
|
|||
|
default: this.sendMessage("Wrong arguments for service on SetService class"); return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|