How to get service name listening at specific port in C#? What you have as input is only two pieces of information: host name and the port number the service is listening at.
Solution
Apparently, .NET does not provide such feature so one needs to stretch a bit to get the answer. What I can suggest (I’m far from saying it’s good approach, though) is to get the name in two steps:
- Use
netstat -a -oand parse the output (ouch!) to get ID of the process (PID) that is listening at given port number - Perform a WMI call to get the name of the service:
SELECT Name FROM Win32_Service where ProcessId = PID
Following this will give you what you want, but to be honest any time I need to parse output to get some information I feel anxious… This is the first place in the code where errors can be introduced.
If there is/are better/safer way(s) to retrieve service name having the host name and port it’s listening at, please share it.


0 Responses to “C#: How to get service name listening at specific port number?”