• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2470
  • Last Modified:

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

0
sonu002
Asked:
sonu002
  • 3
1 Solution
 
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now