0

I have a simple application that gets windows services running on my machine, filter the results by their name and displays service name, port and status of the service.

Right now, I successfully find name and status, but I'm having a hard time finding a way to find a property related to the Port. Do i have access to this info by the method below, or would I need to change my approach entirely?

ManagementObjectSearcher searcher =
    new ManagementObjectSearcher("SELECT * FROM Win32_Service");

ManagementObjectCollection collection = searcher.Get();

foreach (ManagementObject obj in collection)
{
  string name = obj["Name"] as string;

  if (name.StartsWith("something"))
  {
    string pathName = obj["PathName"] as string;
    string status = obj["State"] as string;       
    //string port = ??;

    MyService sc = new MyService() 
        { 
          Name = name,
          Status = status,
          //Port = port,
          Updated = true
        };
  }
}

Thanks!

lucas.mdo
  • 339
  • 7
  • 27
  • A Windows Service isn't required to have a port (such as the Microsoft .NET Framework NGEN services for each version and platform type), since a service may not communicate through TCP/IP at all; A Windows Service may also use more than one, such as IIS Admin (which also manages IIS instances). Normally, the choice of port is up to the service and assumed to be available, so I don't believe they are registered, although the Windows network stack might be able to provide connection information for a given running application. Is there a specific service, or an example? – Matt Jordan Jun 16 '16 at 14:26
  • I see, thanks for the explanation. Yes, services that I am listing are specific as I manually created them through other applications I've developed. As a workaround, I was storing the chosen port at service creation path inside its executable path, but there are some which this approach isn't implemented. – lucas.mdo Jun 16 '16 at 16:13

1 Answers1

0

The command

netstat -a -b

could help. But if effords Administrator rights.

Also have a look at:

Which PID listens on a given port in c#

There are multiple questions like that

Community
  • 1
  • 1
Monarchis
  • 101
  • 6