In this code block we're going to provide you with two methods for creating and removing IIS virtual directories using System.DirectoryServices dll. The DirectoryServices library provides you with a slew of network management features like user, group, domain management, and specifically for this article, IIS administration.
This code could be useful where you want to automate the creation of Websites.
Sub CreateVirtDir(ByVal DirName As String, ByVal PhysicalPath As String)
' reference the default server's root dir
Dim rootDir As New System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
' create a child virtual directory
Dim newDir As System.DirectoryServices.DirectoryEntry = rootDir.Children.Add(DirName, rootDir.SchemaClassName)
' set the physical path
newDir.Properties("Path")(0) = PhysicalPath
' save the directory
newDir.CommitChanges()
End Sub
This next methods deletes the newly created directory.
Sub RemoveVirtDir(ByVal DirName As String)
Dim dir As New System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
dir.Children.Remove(dir.Children.Find(DirName, dir.SchemaClassName))
dir.CommitChanges()
End Sub
Remember in cases where the directory is not a physical directory off of the IIS root, you will need to write a routine to delete the folder.