Access the Services of remote system

Description :


We can control the services of the remote system.
MyServiceController is used to achive this.
Following is the code to access the services remotely

The Namespace part


using System;
using System.ServiceProcess;



Code Part

class MainClass {
public static void ActivateService(string Myserver, string Myservice) {
Console.WriteLine("Status of start the {0} Service", Myservice);
ServiceController MyServiceController;

if (server.Length != 0)
MyServiceController = new ServiceController(Myserver, Myservice);
else
MyServiceController = new ServiceController(Myservice);

MyServiceController.Start();
}

public static void DeactivateService(string Myserver, string Myservice) {
Console.WriteLine("Status of stop the {0} Service", Myservice);
ServiceController MyServiceController;

if (Myserver.Length != 0)
MyServiceController = new ServiceController(Myserver, Myservice);
else
MyServiceController = new ServiceController(Myservice);

MyServiceController.Stop();
}

public static void ShowServices(string Myserver) {
ServiceController[] Myservices;
if (Myserver.Length != 0)
Myservices = ServiceController.GetServices(Myserver);
else
Myservices = ServiceController.GetServices();

foreach (ServiceController MyServiceController in Myservices) {
Console.WriteLine("Found service : {0}", MyServiceController.DisplayName);
}
}


public static void Main(string[] args) {
ActivateService(args[0], args[2]);
DeactivateService(args[0], args[2]);
ShowServices(args[0]);
}
}



Code Explanation

1. create the method ActivateService
2. By using the ServiceController we can start the service of the give server or remote system
3. Create the method DeactivateService
4. By using the ServiceController we can stop the service of the give server or remote system
5. Create the method ShowServices

By
Nathan


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: