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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
sonu002Author Commented:
Also i need it for remote machine
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.