tags:

views:

61

answers:

4

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.

A: 

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?"

Rice Flour Cookies
See here for more useful info: http://www.w3schools.com/asp/coll_servervariables.asp
Rice Flour Cookies
Is you answer about ASP.NET web page?
Roman Prykhodchenko
oh, I see you're running a console program seperate from the web site. For a minute, I thought you were launching the console program from the web site.
Rice Flour Cookies
+1  A: 

you can get with servervariables Request.ServerVariables["SERVER_PORT"]

a52
I need code that works in a console application. Not in an ASP web page.
Roman Prykhodchenko
you can put this information in the web page server... if you can't scan every port, just put that info into app.config
a52
A: 

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.

ggonsalv
Every normal AV kills the process that scans all ports.
Roman Prykhodchenko
A: 

I think I can use System.DirectoryServices for IIS 6 and Microsoft.Web.Administration for IIS 7.

Roman Prykhodchenko