Sunday, March 27, 2011

Hot to get the status of a Windows service in ASP / .net ?

I need to find a way to monitor the status of a list of Windows services over HTTP, preferably without any third party program).

All I really need to be able to do is display the service name and its status ('Started' / 'Stopped').

I'm not an ASP programmer so this is a little outside my realm. I've searched and haven't been able to find much yet.

Any help or suggestions are appreciated.

From stackoverflow
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.ServiceProcess;
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
    
    
            ServiceController[] services = ServiceController.GetServices();
    
            Response.Write("List of running services : <BR>");
            foreach (ServiceController service in services)
            {
    
    
                Response.Write(string.Format(" Service Name: {0} , status {1} <BR>", service.ServiceName, service.Status.ToString()));
    
    
    
            }
        }
    }
    

    just remember to add the system.serviceprocess reference

  • It seems to me that you wish to enumerate services on a REMOTE computer. This can be accomplished using WMI (Windows Management Instrumentation), here's how:

    ConnectionOptions connection = new ConnectionOptions();
    connection.Username = userNameBox.Text;
    connection.Password = passwordBox.Text;
    connection.Authority = "ntlmdomain:DOMAIN";
    
    ManagementScope scope = new ManagementScope("\\\\FullComputerName\\root\\CIMV2", connection);
    scope.Connect();
    
    ObjectQuery query= new ObjectQuery("SELECT * FROM Win32_Service"); 
    
    ManagementObjectSearcher searcher = 
                        new ManagementObjectSearcher(scope, query);
    
    foreach (ManagementObject queryObj in searcher.Get())
    {
         Console.WriteLine("-----------------------------------");
         Console.WriteLine("Win32_Service instance");
         Console.WriteLine("-----------------------------------");
         Console.WriteLine("Caption: {0}", queryObj["Caption"]);
         Console.WriteLine("Description: {0}", queryObj["Description"]);
         Console.WriteLine("Name: {0}", queryObj["Name"]);
         Console.WriteLine("PathName: {0}", queryObj["PathName"]);
         Console.WriteLine("State: {0}", queryObj["State"]);
         Console.WriteLine("Status: {0}", queryObj["Status"]);
    }
    

    This code is taken directly from here, Happy Coding!

0 comments:

Post a Comment