getting the IIS port using System.DirectoryServices

prowla2003

Member
Joined
Jun 18, 2003
Messages
10
Im using this code

Dim oDE As System.DirectoryServices.DirectoryEntry
Dim oDC As System.DirectoryServices.DirectoryEntries
Dim sIISPath As String

sIISPath = "IIS:\\localhost\W3SVC\1"

oDE = New DirectoryServices.DirectoryEntry(sIISPath)
oDC = oDE.Children

and running it in in debug and using the Autos window to try
and find a property somewhere that holds the port that the IIS service is running on and cant..

can someone help.

please!!
 
the answer

C#:
public string getWWWPort(  string sPortType, string sWebSvcInstance) {
	string sw3Site = "IIS://localhost/" + sWebSvcInstance;
	//string sw3Schema = "";
	string sTmp="";
	string [] sTmpArray ;

	if (sPortType.ToUpper() == "SERVERBINDINGS" ) {
		sPortType = "ServerBindings";
	}

	if (sPortType.ToUpper() == "SECUREBINDINGS" ) {
		sPortType = "SecureBindings";
	}

	//DirectoryEntry iisWebSrv = new DirectoryEntry("IIS://localhost/W3SVC/4");
	DirectoryEntry iisWebSrv = new DirectoryEntry(sw3Site);
	PropertyCollection iisWebSrvProperties = iisWebSrv.Properties;

	DirectoryEntry schema = new DirectoryEntry("IIS://localhost/schema/" + iisWebSrv.SchemaClassName);
	PropertyCollection schemaProperties = schema.Properties;

	foreach(string s in schemaProperties["OptionalProperties"]) {
		try {
			if( s == sPortType ) {
				sTmp = iisWebSrvProperties[s].Value.ToString();
				sTmpArray = sTmp.Split(new Char [] {:});
				sTmp = sTmpArray[1];
						
			}
		}

		catch(Exception ex) {

			Console.WriteLine(ex.Message);

		}

	}

	return sTmp;	
}
 
Last edited by a moderator:
Back
Top