Click here to Skip to main content
12,999,417 members (83,835 online)
Rate this:
Please Sign up or sign in to vote.
I have created a windows forms application with a text box for user to enter the port number onto which he wants to create a website. once the user enter the port number(as he wishes) and clicks on a button validation should be made to check in iis whether the port number is already used or not. If its already in use a messagebox should pop up showing an error message that port is already used by another site. I got some sample code from a forum but its not helping me out. Here is the code i have used but i am not able to do the validation.
       string port = textBox2.Text;
       if (!Directory.Exists(port))

         ServerManager serverManager = new ServerManager();
         Site site = serverManager.Sites.FirstOrDefault(s => s.Name == "Racing");
         SiteCollection coll = serverManager.Sites;

         foreach (Site indsite in coll)
             Microsoft.Web.Administration.Binding binding = site.Bindings.Where(b => b.Protocol == "http").FirstOrDefault();
             MessageBox.Show("port number is valid");
 catch (Exception port)

Pls help me out as son as possible. Many Thanks in advance
Posted 15-Apr-13 0:12am
Prasad Khandekar 15-Apr-13 7:18am
You might find the answer to your query here (
shivanand3291 15-Apr-13 7:54am
Thanks prasad. iused this code from the link you hav posted. string hostname = "localhost";
int portno = 9081;
IPAddress ipa = (IPAddress) Dns.GetHostAddresses(hostname)[0];

System.Net.Sockets.Socket sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
sock.Connect(ipa, portno);
if (sock.Connected == true) // Port is in use and connection is successful
MessageBox.Show("Port is Closed");

catch (System.Net.Sockets.SocketException ex)
if (ex.ErrorCode == 10061) // Port is unused and could not establish connection
MessageBox.Show("Port is Open!");
but i am getting error in the 7th line as " This protocol version is not supported here" What can i do now? pls resolve this issue

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170624.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100