Require code for determining service startup type and PID in C#

Hi
I require code to determine service startup type and PID for service running on remote machine. Also i am new to C# so please dont just give suggestions like use WMI I need the code for it. I tried with below code on local machine it gives error not found

public string CheckService(String service)
        {
            try
            {
                ServiceController sc = new ServiceController(service);
                try
                {
                    string path = "Win32_Service.Name='" + service + "'";
                    ManagementPath p = new ManagementPath(path);
                    //construct the management object
                    ManagementObject ManagementObj = new ManagementObject(p);
                    //Console.WriteLine("Path" + p.ToString());
                    
                   // Console.WriteLine("Yippee we got the type"+ManagementObj["StartMode"].ToString());
                }
                catch (System.Exception e)
                {
                    
                    Console.WriteLine("Error in new Code"+e.Message);
                    Console.WriteLine("Error in new Code" + e.StackTrace);
                }
               // Console.WriteLine("The " + sc.DisplayName + " service status is currently set to " + sc.Status.ToString() + ".\r\n");
                
                
                
                return "Hello";
            }

Open in new window

sonu002Asked:
Who is Participating?
 
jandromedaCommented:
Try this code.
ManagementClass class1 = new ManagementClass("Win32_Service");
 
          	foreach(ManagementObject ob in class1.GetInstances())
          	{
          		if (ob.GetPropertyValue("Name").ToString() == "gusvc")
          		{
          			Console.WriteLine(ob.GetPropertyValue("Caption"));
          			Console.WriteLine(ob.GetPropertyValue("ProcessID"));
          			Console.WriteLine(ob.GetPropertyValue("StartMode"));
          			Console.WriteLine(ob.GetPropertyValue("StartName"));
                        }
                }

Open in new window

0
 
sonu002Author Commented:
Hi by the solution above i dont get any exceptions but no result also just a blan k window
The OS is Win XP is there any modification requied for that
0
 
sonu002Author Commented:
Also i need it for remote machine
0
 
sonu002Author Commented:
Hi I got the answer through some other forum ! the second solution was good but the remote machine part was not described although i know it is done thr WMI... please answer all question to proper details
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.