ASP.NET application and windows services

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to start or stop a windows service from ASP.NET 2.0?
Some examples?

thanks
Przemo
 
Namespace: System.ServiceProcess
Assembly: System.ServiceProcess (in system.serviceprocess.dll)

Add a reference to this assembly in your web project, create a
ServiceProcess object and go to town. Some sample code goodness:

// Toggle the Telnet service -
// If it is started (running, paused, etc), stop the service.
// If it is stopped, start the service.
ServiceController sc = new ServiceController("Telnet");
Console.WriteLine("The Telnet service status is currently set to {0}",
sc.Status.ToString());

if ((sc.Status.Equals(ServiceControllerStatus.Stopped)) ||
(sc.Status.Equals(ServiceControllerStatus.StopPending)))
{
// Start the service if the current status is stopped.

Console.WriteLine("Starting the Telnet service...");
sc.Start();
}
else
{
// Stop the service if its status is not set to "Stopped".

Console.WriteLine("Stopping the Telnet service...");
sc.Stop();
}

// Refresh and display the current service status.
sc.Refresh();
Console.WriteLine("The Telnet service status is now set to {0}.",
sc.Status.ToString());
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top