. Net has actually done a good job for us in this regard. FCL provides a lot of classes to help us complete this task, making our development work very simple and happy. Programming to control IIS is actually very simple, just like ASP ,. in Net, you need to use ADSI to operate IIS, but at this time we no longer need GetObject, because. net provides us with more powerful new features.
System. the DirectoryServices namespace includes some powerful Dongdong-DirectoryEntry and DirectoryEntries, which provide us with powerful functions to access the Active Directory, these classes allow us to operate IIS, LDAP, NDS, and WinNT with powerful functions :)
However, here we only talk about the control of IIS. In general, operations on IIS are generally performed on virtual directories, so I will refer to this column as the main content.
First of all, we need to figure out the problem of the IIS hierarchy. Below is a picture I have obtained from abroad, which explains the IIS hierarchy well:
[HtmChina: Image id = Image1 | 12] [/htmChina: Image]
To understand the control Syntax of IIS, we must understand the hierarchical structure of IIS metadata (Metabase. Each node in the figure is called a Key, and each Key can contain one or more values. These values are what we call properties ), the Key in the IIS metadata is consistent with the element in IIS. Therefore, setting the attribute value in the metadata affects the setting in IIS. This is the basic idea and core of our programming.
We also need to understand the concept of Schema. It indicates the name of the Framework in IIS, that is, it can understand the Key type in IIS metadata. Specifically, it refers to the type of each node. We know that IIS has virtual directories, common directories, and files, all of which belong to the IIS elements, and their labels are Schema. For example, the Schema of the virtual directory is \ "IIsVirtualDir \", and the common directory is \ "IIsWebDir \". In this way, when we add or delete directories, IIS will know whether we have added virtual directories or normal directories.
Create virtual directory
DirectoryEntry is a big gift from. Net. We know its function-directory entry. All users who have used ADSI know that when operating IIS and WinNT, we also need to provide their Path. When operating IIS, the Path format is:
IIS: // ComputerName/Service/Website/Directory
ComputerName: the name of the server to be operated. It can be either a name or an IP address, and localhost is often used.
Service: the operating server. IIS has Web, FTP, and SMTP services. We mainly operate on the Web functions of IIS. Therefore, \ "W3SVC \", for FTP, \ "MSFTPSVC \"
WebSite: an IIS service can contain many sites, which are used to set up sites for operations. The value is a number. The default value is 1, indicating the default site. If there are other sites, the number starts from 1 and so on.
Directory: The Directory Name of the operation. The top-level Directory of a website is \ "ROOT \", and other directories are its children ).
First, we obtain the top-level directory (root directory) of a Website ):
DirectoryEntry rootfolder = new DirectoryEntry (\ "IIS: // localhost/W3SVC/1/ROOT \");
If no exception occurs when we create this object, the directory actually exists. [Page]
Next we will 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 simple, that is, in the root directory subset (rootfolder. add another record. The Add method in the DirectoryEntries class is used here. It returns a DirectoryEntry, indicating the newly added directory. The first parameter is the name of the virtual directory, the second is the Schema class name to indicate the directory type we add. Then, use the Invoke method of DirectoryEntry to call the \ "AppCreate \" method in ADSI to create the directory (it seems that the directory can be created successfully without this step, but for the sake of security, you can call the CommitChanges method in the new and root directories to confirm the operation.
When creating a new directory, we can also assign values to the attributes of this directory. However, my practical experience tells me that it is best not to do this. If you assign values when creating a directory, there will be many attributes that cannot be assigned successfully, such as the Path attribute that represents the real directory. Therefore, it is recommended that you create a directory first and then assign a value to update the directory information.
Update virtual directory
I believe you are familiar with IIS and understand some important settings in IIS, such as AccessRead, AccessWrite, and AccessExecute. You can assign values to the Properties attribute set of DirectoryEntry. You can assign values in two ways:
The first method is to call the Add method of the Properties set, for example:
Dir. Properties [\ "AccessRead \"]. Add (true );
The second is to assign values to the first index:
Dir. Properties [\ "AccessRead \"] [0] = true;
Both methods are feasible. It depends on your preferences.
Before assigning values, we still need to determine the target to assign values. :) here we use the Find method of the DirectoryEntries class, for example:
DirectoryEntry de = rootfolder. Children. Find (\ "Aspcn \", \ "IIsVirtualDir \");
After finding it, we can assign values. Make sure you have a good look when assigning values. The virtual directory has a large number of attribute values .. (Too many, I will not repeat the flying knife. You can check it on Microsoft's site :)
Commonly used: AccessRead, AccessWrite, AccessExecute, AccessScript, DefaultDoc, enabledefadoc doc, Path
Delete virtual directory
The method to delete a virtual directory is also very simple, that is, to 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. [Page]
Object [] paras = new object [2];
Paras [0] = \ "IIsWebVirtualDir \"; // indicates that a virtual directory is operated.
Paras [1] = \ "Aspcn \";
Rootfolder. Invoke (\ "Delete \", paras );
Rootfolder. CommitChanges ();
Which one you like depends on programming habits :))
About the classes I wrote
The class library I wrote further simplifies this. You only need to call the Connect () method to directly operate the Create and Delete methods. The program can be further simplified and batch operations are supported.