In: Computer Science
write a c# console application app that reads all the services in the task manager and automatically saves what was read in a Notepad txt file. Please make sure that this program runs, also add some comments
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
using System;
using System.ServiceProcess; //for fetching
services
using System.IO; //for writing to file
class FetchServices
{
static void
Main(string[] args)
{
//getting
an array of ServiceController objects which are the services
currently
//installed
on this
computer
ServiceController
[] services =
ServiceController.GetServices();
//opening
a file named services.txt for writing. change the file path or
name
//if
you want to.
string
filename="services.txt";
StreamWriter
writer=new
StreamWriter(filename);
//looping
through each service in the array
for(int
i=0;i<services.Length;i++){
//if
service is currently running, writing the name of service to
the
//output
file. if you want to write all services instead of currently
//running
ones, remove the if statement and use only the below line
//writer.WriteLine(services[i].DisplayName);
if(services[i].Status==ServiceControllerStatus.Running){
writer.WriteLine(services[i].DisplayName);
}
}
//closing
the writer, saving changes
writer.Close();
//alerting
user to check services.txt and exiting
Console.WriteLine("services
list has been written to the file: "+filename+", please
check.");
}
}
/*generated services.txt file screenshot (partial)*/