I need to get a tcp port of the specified web site on IIS 7 and IIS 6 using C#. I have a console application that knows the web site's name. It should find a port this web site is served on.
OK. I'm going to give you a different answer since you commented that my last answer was not the answer to your question.
Try adding a global.asax
file to your asp.net application. It will have functions to handle different events on the server. For the Application_Start
function, you can have some code to save the port number that the web site is running on in a file or database somewhere.
Then, the console application can access the same file or database to find the port number.
If that doesn't suit you, then perhaps a better question to ask on SO would be "How can I programmatically read the IIS settings for a web site at run time?"
you can get with servervariables Request.ServerVariables["SERVER_PORT"]
By default IIS binds to port 80 (default http port) but I am sure the answer is not that simple.
Maybe you could have used the admin scripts in IIS 6.0, to iterate through the IIS objects to find the port number, but this assumes the script is physically running on the server.
The only other option is run scan of each 65535 port to see if there a html listener using wget maybe.
I think I can use System.DirectoryServices for IIS 6 and Microsoft.Web.Administration for IIS 7.