Manipulate Virtual Directories via System.DirectoryServices namespace
Just a quick note on manipulating IIS virtual directories from .NET code:
I was writing a Custom Action class for my solution's web setup project, and was not sure how to best go about removing the already created virtual directory on Rollback or Uninstall. I at first looked at Windows Server 2003's IISDIR utility, or using IIS's Admin scripts .vbs files located at C:\inetpub\Adminscripts, but these solutions weren't providing me the cross-operating system solution I required.
I came across this post referring to a under-used namespace: System.DirectoryServices. You'll have to add reference to the System.DirectoryServices.dll for your project. After I discovered this, creating and deleting virtual directories from the IIS MetaBase.bin file is a snap:
/// <summary>
/// Remove Virtual Directory from IIS.
/// </summary>
/// <param name="VirtualDirName"></param>
private void DeleteVirtualDirectory(string VirtualDirName)
{
DirectoryEntry Parent = new DirectoryEntry(@"IIS://localhost/W3SVC/1/Root");
Object[] Parameters = { "IIsWebVirtualDir", VirtualDirName };
Parent.Invoke("Delete", Parameters);
}
I'm planning on posting a few more custom action install tips very shortly, stay tuned.
steve