Link to home
Start Free TrialLog in
Avatar of sonu002
sonu002

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of jandromeda
jandromeda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sonu002
sonu002

ASKER

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
Avatar of sonu002

ASKER

Also i need it for remote machine
Avatar of sonu002

ASKER

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