Programming to control IIS is actually very simple, like the ASP. NET, you need to use ADSI to manipulate IIS, but at this point we no longer need GetObject because. NET provides us with new things that are more powerful.
The System.DirectoryServices namespace includes powerful--directoryentry,directoryentries that provide us with the powerful capabilities to access the Active Directory, which allows us to manipulate IIS, LDAP, NDS and Winnt.
However, we only talk about the control of IIS, in general, we operate IIS is generally the operation of the virtual directory, so I put this as the main content.
First of all, we need to figure out the hierarchy of IIS, and here's a map I found from abroad, which explains the hierarchy of IIS:
[Htmchina:image id=image1|12] [/htmchina:image]
To get a sense of the control syntax of IIS, we have to figure out the hierarchy of IIS metadata (Metabase) on the diagram above. Each node in the graph is called the key, each key can contain one or more values, which are the properties we say, and the keys in the IIS metadata match the elements in IIS, so setting the property values in the metadata affects the settings in IIS. This is the basic idea and core of our programming.
Also understand the concept of schema. It represents the name of the schema in IIS, which can understand the type of key in the IIS metadata, specifically the type of each node. We know that IIS has virtual directories, plain directories, and files, all of which are elements of IIS that distinguish their flags from schemas. For example, the schema of the virtual directory is "IIsVirtualDir", the Ordinary Directory is "Iiswebdir". So when we add and delete directories, IIS knows that we are adding a virtual directory or a normal directory.
Create a virtual directory
DirectoryEntry is a great gift from. NET, his name we know his function--directory entrance. People who have used ADSI know that when we operate iis,winnt these, we also need to provide their path, and when we operate IIS, the path is in the format:
Iis://computername/service/website/directory
ComputerName: That is, the name of the server to operate, can be the name can also be IP, often used is the localhost
Service: That is, the operation of the server, IIS has the Web, also has FTP, as well as SMTP these services, we mainly operate IIS Web functions, so here is "w3svc", if it is ftp should be "MSFTPSVC"
WebSite: An IIS service can include many sites, which are used to set up operations for the site. His value is a number, the default is 1, the default site, and if there are other, then start from 1 and so on.
Directory: Needless to say, the directory name of the operation, the general top-level directory of a site is "ROOT", and other directories are his children (child).
First we get the top-level directory (root directory) of a site:
DirectoryEntry RootFolder = new DirectoryEntry ("Iis://localhost/w3svc/1/root");
If we create this object without an exception, it means that the directory is real.
Let's add a new virtual directory, such as "ASPCN":
DirectoryEntry Newvirdir = RootFolder. Children.add ("ASPCN", "IIsWebVirtualDir");
Newvirdir.invoke ("AppCreate", true);
Newvirdir.commitchanges ();
Rootfolder.commitchanges ();
The idea of creating a directory is simple, in a subset of the root directory (RootFolder. Children) to add a record, which uses the Add method in the Directoryentries class, which returns a DirectoryEntry that represents the newly added directory, the first parameter is the name of the virtual directory, The second is the class name of the schema to indicate the type of directory we are adding. Then, using the DirectoryEntry invoke method, call the "AppCreate" method in ADSI to actually create the directory (it doesn't seem to take this step to create a directory success, but for the sake of insurance, let's use it), and finally, call the new, The CommitChanges method of the root directory to confirm this operation.
When creating a new directory, we can also assign values to the attributes of the directory, but my actual combat experience tells me that it is best not to do so, and that if you assign values at creation time, there will be many attributes that cannot be assigned success, such as an important path attribute that represents the real directory. Therefore, the flying knife suggested that it is best to create a directory, and then assign a value, that is, update the directory information.