Help with a C# problem? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help with a C# problem?

Hi there, I am attempting to check the status of a Windows Service through the console in C#, however it is throwing an error that I'm not sure of the reason for. Here is the error: http://i.imgur.com/xdFNu5X.png I have hardcoded "Superfetch" for testing, and this can be seen clearly as a service running in windows. The Superfetch service running: http://i.imgur.com/y14BQkf.png Any insight would be appreciated! Thanks.

30th Jun 2017, 9:21 PM
Shannon
Shannon - avatar
5 Answers
0
Your code looks good. You may want to catch that exception and run the program with CTRL+F5. It may be a problem with the initialization. using System; using System.Collections.Generic; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { ServiceCheck("Themes"); } static void ServiceCheck(String service) { try { ServiceController sc = new ServiceController(service); Console.WriteLine("The "+ service+" status is currently set to {0}", sc.Status.ToString()); } catch(Exception huh) { Console.WriteLine("Exception Thrown!"+ huh); } } } }
2nd Jul 2017, 8:48 AM
Claudiu Stefan Haidu
Claudiu Stefan Haidu - avatar
0
Thank you for your suggestion, I managed to figure out that I was using the wrong name for services! "Superfetch" has a service name of "SysMain", which is why my code didn't work! My line sc.ServiceName = service is what caused this error, as I should be turning "Superfetch" into "SysMain"(which can be read by the Windows system.) Thank you for your input! @ClaudiuStefanHaidu
2nd Jul 2017, 10:34 PM
Shannon
Shannon - avatar
0
Well, those kind of errors occur sometimes and it's really important not to have them and detect them before they hurt you. For that you can also try using an app to help you. I tend to use checkamrx for code security and it works great. Good luck. Ben.
15th Jul 2017, 9:07 PM
Ben hart
0
void Func { Console.Write("Hi there"); } static void Main(string[] args) { Func(); }
7th Jan 2018, 10:05 PM
Abdelouahad Mirar
Abdelouahad Mirar - avatar
0
static void Func { Console.Write("Hi there"); } static void Main(string[] args) { Func(); }
31st Oct 2020, 2:15 PM
RIZKI HERI PURWANTO
RIZKI HERI PURWANTO - avatar