code_junkie

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

Nov 13th, 2011
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.ServiceProcess;
  8.  
  9. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13.  
  14.  
  15.  
  16. ServiceController[] services = ServiceController.GetServices();
  17.  
  18. Response.Write("List of running services : <BR>");
  19. foreach (ServiceController service in services)
  20. {
  21.  
  22.  
  23. Response.Write(string.Format(" Service Name: {0} , status {1} <BR>", service.ServiceName, service.Status.ToString()));
  24.  
  25.  
  26.  
  27. }
  28. }
  29. }
  30.  
  31. ConnectionOptions connection = new ConnectionOptions();
  32. connection.Username = userNameBox.Text;
  33. connection.Password = passwordBox.Text;
  34. connection.Authority = "ntlmdomain:DOMAIN";
  35.  
  36. ManagementScope scope = new ManagementScope("\\FullComputerName\root\CIMV2", connection);
  37. scope.Connect();
  38.  
  39. ObjectQuery query= new ObjectQuery("SELECT * FROM Win32_Service");
  40.  
  41. ManagementObjectSearcher searcher =
  42. new ManagementObjectSearcher(scope, query);
  43.  
  44. foreach (ManagementObject queryObj in searcher.Get())
  45. {
  46. Console.WriteLine("-----------------------------------");
  47. Console.WriteLine("Win32_Service instance");
  48. Console.WriteLine("-----------------------------------");
  49. Console.WriteLine("Caption: {0}", queryObj["Caption"]);
  50. Console.WriteLine("Description: {0}", queryObj["Description"]);
  51. Console.WriteLine("Name: {0}", queryObj["Name"]);
  52. Console.WriteLine("PathName: {0}", queryObj["PathName"]);
  53. Console.WriteLine("State: {0}", queryObj["State"]);
  54. Console.WriteLine("Status: {0}", queryObj["Status"]);
  55. }
Add Comment
Please, Sign In to add comment