/// <summary>
/// This function creates an virtual directory on an IIS site
/// </summary>
/// <param name="parentPath">The path to the directory (without the directory name)</param>
/// <param name="name">The name of the directory</param>
public void CreateWebDirectory(string parentPath, string name)
{
try
{
if (EntryExists(parentPath + "/" + name))
{
//Virtual dirctory already exists on IIS, there is no need to create one
return;
}
using (DirectoryEntry entry = new DirectoryEntry(parentPath))
{
//Invoking the "Create" method which creates the virtual directory
DirectoryEntry newEntry = (DirectoryEntry)entry.Invoke("Create", new
object[2] { "IIsWebDirectory", name });
newEntry.CommitChanges();
}
}
catch (Exception ex)
{
//Do some exception handling
}
}
/// <summary>
/// This fuction check if a virtual direcory exists in IIS
/// </summary>
/// <param name="path">The path to the virtual directory</param>
/// <returns>True if exists False otherwise</returns>
public bool EntryExists(string path)
{
try
{
using (DirectoryEntry entry = new DirectoryEntry(path))
{
return entry.Guid != Guid.Empty;
}
}
catch
{
return false;
}
}