Delete Virtual Directory and related Folder
System.DirectoryServices.DirectoryEntry iISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/W3SVC/1/Root");
string sWebPath = iISAdmin.Properties["Path"].Value.ToString();
//If the virtual directory already exists then delete it
foreach(System.DirectoryServices.DirectoryEntry vd in iISAdmin.Children)
{
if(vd.Name==sAppName)
{
sWebPath += "\\" + vd.Name;
*****
sWebPath=vd.Properties["Path"].Value.ToString();
iISAdmin.Invoke("Delete", new string[]{vd.SchemaClassName, sAppName}); System.IO.Directory.Delete(sWebPath); iISAdmin.CommitChanges();
break;
}
}
I like to check whether "Path" property of vd is exists or not. like the following coding in ***** position.
if(((System.DirectoryServices.PropertyCollection)((vd.Properties))).valueTable.Count > 0 )
I wrote this coding in windows application.
[992 byte] By [
Sync] at [2007-11-11 7:18:39]

# 3 Re: Delete Virtual Directory and related Folder
The virtual directory that is created by Visual Studio 2003, is lit bit different than the virtual directory that is made as Web Sharing from Folder Properties.
I found one useful facts after I have checked the differences between two types of virtual directories using metaedit2.2. If the vd is created by VS2003, the data of "keyType" Key of this virtual directory will be "IIsWebDirectory". Likewise, If the vd is made as Web Sharing, the data of "keyType" Key of this virtual directory will be "IIsWebVirtualDir". So, we can know whether we need to check the path property or not by checking the data of "KeyType".
Deleting Virtual Directory...
System.DirectoryServices.DirectoryEntry iISAdmin = new System.DirectoryServices.DirectoryEntry("IIS://" + sWebSite + "/W3SVC/1/Root");
string sWebPath = iISAdmin.Properties["Path"].Value.ToString();
//If the virtual directory already exists then delete it
foreach(System.DirectoryServices.DirectoryEntry vd in iISAdmin.Children)
{
if(vd.Name==sAppName)
{
sWebPath += "\\" + vd.Name;
if(vd.KeyType == "IIsWebVirtualDir")
sWebPath=vd.Properties["Path"].Value.ToString();
iISAdmin.Invoke("Delete", new string[]{vd.SchemaClassName, sAppName});
System.IO.Directory.Delete(sWebPath);
iISAdmin.CommitChanges();
break;
}
}
Sync at 2007-11-11 21:51:17 >
