Baris ERGUN

I am trying to get the Port and the Ip Address of the IIS server from a Windows Application. For your information the windows application works on the same PC which is IIS installed.

I have looked WMI and some ISAPI but I could not find the exact answer.

Thanks




Re: .NET Framework Networking and Communication Get IIS Variables from C# Windows Forms


Re: .NET Framework Networking and Communication Get IIS Variables from C# Windows Forms

Baris ERGUN

Thank you for your reply but I know all these articles. These only help me getting the Ip Address of the server . What I am trying to do is to find the IIS Default Web Site TCP Port.

There is a way to do this with Directory Entry Class but I still did not find the solution






Re: .NET Framework Networking and Communication Get IIS Variables from C# Windows Forms

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

}