Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Using System.DirectoryServices to Access ASDI provider for IIS in VB.NET (Part I)

Author: Aadil

Reading Properties of IIS Objects

The System.DirectoryServices namespace provides the programmer access to any directory services including the directory service for IIS. The ADSI provider for IIS is available in Windows XP and Windows 2003 Server.

 

Behind the scenes the System.DirectoryServices access the low level COM API for its operation. This namespace is just a wrapper (very flexible and object oriented one) on the COM objects.

 

Each object or entry in the IIS directory is represented as a DirectoryEntry object and to get the whole web server on a specific machine in the DirectoryEntry you need to specify the IP address or the computer name running IIS.

 

Dim webServer As New DirectoryEntry("IIS://localhost/W3SVC")

 

Note : (I’ve tried to open the DirectoryEntry for another machine by providing extra parameters in the DirectoryEntry object’s constructor overloads but they don’t work that easily especially the username and password parameters won’t work for ADSI provider for IIS)

 

Each directory entry is associated with two main properties. One is the Children property which represents all the child objects present under this specific DirectoryEntry and the other is the Properties property which is an object of type PropertyCollection and contains all the properties of this DirectoryEntry object. Also there is no sure of which properties are present for a DirectoryEntry object, you need to manually specify the name or index of a property which you need to access.

 

The webServer object created above can contain many types of children such as Websites, SMTP Servers or FTP Servers etc. In the following code we will enumerate through all the children of the webServer object which are of types Websites and get a reference to that website whose name (Sever Comment) is “MyWeb”.

Dim webServer As New DirectoryEntry("IIS://localhost/W3SVC")

 

    Dim webSite As DirectoryEntry

    For Each webSite In webServer.Children

      If webSite.SchemaClassName = "IIsWebServer" Then

        'Check to see if the current webSite is what you want

        If webSite.Properties("ServerComment")(0) = "MyWeb" Then

          Exit For

        End If

      End If

    Next

 

    'Now here the webSite DirectoryEntry object contains a reference to the desired website

    Dim StrInfo As New StringBuilder

 

    StrInfo.Append("WebSite Name : " & webSite.Properties("ServerComment")(0) & vbCrLf)

    StrInfo.Append("Default Documents : " & webSite.Properties("DefaultDoc")(0) & vbCrLf)

 

    MessageBox.Show(StrInfo.ToString())

Virtual directories are also represented by objects of type DirectoryEntry and there is a special Virtual Directory that represents the root of a website. To get access to this object we will enumerate through the Children property of this website object.

 

StrInfo = New StringBuilder

 

    Dim vdRoot As DirectoryEntry

    For Each vdRoot In webSite.Children

      If vdRoot.SchemaClassName = "IIsWebVirtualDir" Then

        StrInfo.Append("Virtual Directory Name : " & vdRoot.Name & vbCrLf)

        StrInfo.Append("Website Root Path : " & vdRoot.Properties("Path")(0) & vbCrLf)

      End If

    Next

 

    MessageBox.Show(StrInfo.ToString())

 

This will show information about the root virtual directory. The properties are coming from the properties of the IIS Metabase. For more information about the Metabase properties of IIsWebServer entry refer to the following URL

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/iissdk/html/1103907a-56ab-4b8d-84b6-5a73bd8532ef.asp


Article Comments
Good Day!!! www.devasp.net is one of the most outstanding innovative websites of its kind. I take advantage of reading it every day. I will be back.

Posted on 12/30/2009 8:14:32 AM by bad credit loans

The author of www.devasp.net has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: Any time you laugh so hard that the milk you are drinking comes out of your nose, a cute member of the opposite sex will walk by and see it. Thanks for the info.

Posted on 1/24/2010 12:36:10 AM by payday loans

I am bare impressed with the article I have just read. I wish the author of www.devasp.net can continue to provide so much useful information and unforgettable experience to www.devasp.net readers. There is not much to say except the following universal truth: My name is alex I will be back.

Posted on 2/12/2010 2:10:05 AM by online payday loans

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Loop through all sub categories and display all files under these using Visual Basic.Net

Disclaimer - Privacy
© 2002-2017 DevASP.net