Baris ERGUN
The below line gets the IIS Port Number using System.DirectoryServices.dll the path IIS://localhost/w3svc/1 means the Defult Web Site and ServerBindings Prop gets IpAddress:Port:Description so that to get the Port you have to read the line between 2 ":" marks.
String sWebSite = "w3svc/1";
DirectoryEntry site =
new DirectoryEntry("IIS://localhost/" + sWebSite );
String sample= site.Properties["ServerBindings"][0].ToString();
int firstQuoteIndex = sample.IndexOf(":");
int secondQuoteIndex = sample.IndexOf(":",firstQuoteIndex+1 );
string lastFound = sample.Substring(firstQuoteIndex +1, secondQuoteIndex -firstQuoteIndex-1);
MessageBox.Show(lastFound);
And this below code snippet will help you get all the Server Variables
foreach (string propertyName in site.Properties.PropertyNames)
{
this.textBox1.Text += propertyName;
PropertyValueCollection pvc = site.Properties[propertyName];
foreach(object Value in pvc)
this.textBox1.Text += " " + Value.ToString();
this.textBox1.Text += "\n";
}