.Net actually does this very well for us. FCL provides many classes to help us complete this work, making our development work very simple and happy. Programming to control IIS is actually very simple. Like ASP, .Net needs to use ADSI to operate IIS, but at this time we no longer need GetObject, because .Net provides us with new stuff with more powerful functions.
The System.DirectoryServices namespace includes some powerful things - DirectoryEntry, DirectoryEntries, which provide us with powerful functions to access Active Directory. These classes allow us to operate IIS, LDAP, NDS and WinNT. They are very powerful. :)
However, we only talk about the control of IIS here. Generally speaking, when we operate IIS, we usually operate virtual directories, so I will list this as the main content.
First of all, we need to understand the hierarchical structure of IIS. The following is a picture I found from abroad, which explains the hierarchical structure of IIS very well:
[htmChina:Image id=Image1|12][/htmChina:Image]
In order to understand the control syntax of IIS, we must understand the above figure and understand the hierarchical structure of IIS metadata (Metabase). Each node in the graph is called a Key, and each Key can contain one or more values. These values are what we call properties. The Key in IIS metadata is consistent with the elements in IIS, so the metadata The setting of attribute values in the data will affect the settings in IIS. This is the basic idea and core of our programming.
In addition, you also need to understand the concept of Schema. It represents the name of the architecture in IIS, that is, you can understand the type of Key in IIS metadata, specifically the type of each node. We know that there are virtual directories, ordinary directories, and files in IIS, and these are all elements of IIS, and the label that distinguishes them is Schema. For example, the Schema of the virtual directory is "IIsVirtualDir" and the normal directory is "IIsWebDir". In this way, when we add or delete directories, IIS will know whether we are adding a virtual directory or a normal directory.
Creating a virtual directory
DirectoryEntry is a great gift that .Net gives us. We know its function just by its name - directory entry. Anyone who has used ADSI knows that when operating IIS and WinNT, we also need to provide their Path. When operating IIS, the format of this Path is:
IIS://ComputerName/Service/Website/Directory
ComputerName: that is, the operating server The name can be a name or an IP. The most commonly used one is localhost.
Service: The server that is operated. There are Web, FTP, and SMTP services in IIS. We mainly operate the Web function of IIS, so here is "W3SVC". If it is FTP, it should be "MSFTPSVC"
WebSite: An IIS service can include many sites. This is the site used to set up operations. Its value is a number, the default is 1, indicating the default site, if there are others, start from 1 and so on.
Directory: Needless to say, it is the name of the directory for operation. The top directory of a site is generally "ROOT", and other directories are its children.
First, we get the top-level directory (root directory) of a site:
DirectoryEntry rootfolder = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT");
If no exception occurs when we create this object, it means that this directory is real exist.
Let's add a new virtual directory. For example, we want to add "Aspcn":
DirectoryEntry newVirDir = rootfolder.Children.Add("Aspcn", "IIsWebVirtualDir");
newVirDir.Invoke("AppCreate",true);
newVirDir.CommitChanges();
rootfolder.CommitChanges();
The idea of creating a directory is very simple, that is, add another record to a subset of the root directory (rootfolder.Children). The Add method in the DirectoryEntries class is used here. It returns a DirectoryEntry, which represents the newly added directory. One parameter is the name of the virtual directory, and the second is the class name of the Schema to indicate the type of directory we added. Then use the Invoke method of DirectoryEntry to call the "AppCreate" method in ADSI to actually create the directory (it seems that the directory can be created successfully without taking this step, but for the sake of safety, everyone should use it), and finally call the new, The CommitChanges method of the root directory confirms this operation.
When creating a new directory, we can also assign values to the attributes of this directory at the same time, but my practical experience tells me that it is best not to do this. If we assign values when creating a new directory, there will be many attributes that cannot be assigned successfully. For example, important ones represent true The Path attribute of the directory. Therefore, Feidao recommends that you create the directory first and then assign values, that is, update the directory information.
Update the virtual directory
. I believe everyone is familiar with IIS and understands some important settings in IIS, such as readable (AccessRead), writable (AccessWrite), executable (AccessExecute), etc. These can be achieved by assigning values to the Properties property collection of DirectoryEntry. Assignment can be done in two ways:
the first is to call the Add method of the Properties collection, such as:
dir.Properties["AccessRead"].Add(true);
the second is to assign a value to the first index value:
dir. Properties["AccessRead"][0] = true;
Both methods are possible. It depends on your preference.
Before assigning a value, we still need to determine the target to be assigned:) Here we use the Find method of the DirectoryEntries class, such as:
DirectoryEntry de = rootfolder.Children.Find("Aspcn", "IIsVirtualDir");
Once found, we It’s ready for assignment. Be sure to take a good look when assigning values. There can be many attribute values in a virtual directory, and you can check a lot of them. . : (There are too many, and I won’t repeat them. You can check them on Microsoft’s website:)
The more commonly used ones are: AccessRead, AccessWrite, AccessExecute, AccessScript, DefaultDoc, EnableDefaultDoc, Path.
The method ofdeleting
a virtual directory is also It's very simple, just find the virtual directory you want to delete, and then call the AppDelete method.
DirectoryEntry de = rootfolder.Children.Find("Aspcn","IIsVirtualDir");
de.Invoke("AppDelete",true);
rootfolder.CommitChanges();
Another method is to call the Delete method of the Root directory.
object[] paras = new object[2];
paras[0] = "IIsWebVirtualDir"; //Indicates that the operation is a virtual directory
paras[1] = "Aspcn";
rootfolder.Invoke("Delete",paras);
rootfolder.CommitChanges();