In the process of system development, we often encounter using asp.net to operate IIS, such as creating a new virtual directory, changing the properties of the virtual directory, deleting the virtual directory, etc. The current analysis is as follows:
//If the virtual directory is named " Webtest", first reference it in the project
//System.DirectoryServices.dll, then
using System.DirectoryServices;
protected System.DirectoryServices.DirectoryEntry dirroot;
1. Add a new virtual directory
DirectoryEntry newVirDir = dirroot.Children.Add("Webtest","IIsWebVirtualDir");
newVirDir.Invoke("AppCreate",true);
newVirDir.CommitChanges();
dirroot.CommitChanges();
2. Change the virtual directory attributes
//The most commonly used attributes of the virtual directory are: AccessRead, AccessWrite, AccessExecute, AccessScript, DefaultDoc, EnableDefaultDoc, Path, etc.
DirectoryEntry Dirport = dirroot.Children.Find("Webtest", "IIsVirtualDir");
Dirport .Properties["AccessRead"][0] = true;
3. Delete the virtual directory
DirectoryEntry Dirport = dirroot.Children.Find("Webtest","IIsVirtualDir");
Dirport.Invoke("AppDelete",true);
dirroot.CommitChanges();
or:
object[] part = new object[2];
part[0] = "IIsWebVirtualDir";
part[1] = "Webtest";
dirroot.Invoke("Delete",part);
dirroot.CommitChanges();