How to monitor windows services using c# -
how can monitor windows services using c# , have save services name, started time , services end time using in csv file. if new services started should automatically write services name, started time , services end time using in existing csv file.
you can list running services using servicecontroller
or managementobjectsearcher
.
here sample using managementobjectsearcher
:
using system.management; ... stringbuilder sb = new stringbuilder(); string format = "{0},{1},{2},{3},{4}"; // header line sb.appendformat(format, "displayname", "servicename", "status", "processid", "pathname"); sb.appendline(); managementobjectsearcher searcher = new managementobjectsearcher("select * win32_service"); foreach( managementobject result in searcher.get() ) { sb.appendformat(format, result["displayname"], result["name"], result["state"], result["processid"], result["pathname"] ); sb.appendline(); } file.writealltext( @"c:\temp\managementobjectsearcher_services.csv", sb.tostring() );
for getting start , stop times looks have query windows event log.
this blog post show how can monitor event log notified when service stopped or started: https://dotnetcodr.com/2014/12/02/getting-notified-by-a-windows-service-status-change-in-c-net/
Comments
Post a Comment