. How to operate the IIS virtual directory Principle Analysis and Realization Scheme _ practical skills in net

Source: Internet
Author: User
. NET has actually done a good job for us in this area. FCL offers a number of classes to help us complete this work, making our development work very simple and enjoyable. 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 to getobject this stuff, because NET provides us with more powerful new stuff.

The System.DirectoryServices namespace includes powerful Dongdong--directoryentry, Directoryentries, which provide us with the powerful capabilities to access the Active Directory, which allows us to manipulate IIS, LDAP , NDS, and Winnt, the function is very powerful bar:
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:
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:
Copy Code code as follows:

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 a top-level directory of a site (root directory)
Copy Code code as follows:

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":
Copy Code code as follows:

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 then call the new, root directory in turn CommitChanges method 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.

Update virtual directory
I am sure you are familiar with IIS, and understand some important settings in IIS, such as readable (AccessRead), writable (AccessWrite), executable (AccessExecute), and so on. These can be achieved by assigning values to the Properties collection of the DirectoryEntry property. Assignments can be done in two ways:
The first is to invoke the Add method of the Properties collection, such as:
Copy Code code as follows:

Dir. properties["AccessRead"]. ADD (TRUE);

The second is to assign a value to the first index value:
Copy Code code as follows:

Dir. properties["AccessRead"][0] = true;

Both of these methods are feasible. It depends on your liking.
We still have to determine the target to be assigned before we do the assignment: Here we use the Find method of the Directoryentries class, such as:
Copy Code code as follows:

DirectoryEntry de = RootFolder. Children.find ("ASPCN", "IIsVirtualDir");

If we find it, we can assign a value. You must take a good look at the value of the virtual directory, the attribute value can be more than a large pile: (Too much, flying knives I do not repeat, we go to Microsoft's site to check:)
More commonly used are: Accessread,accesswrite,accessexecute,accessscript,defaultdoc,enabledefaultdoc,path

Delete virtual directory
The way to delete a virtual directory is also simple: Find the virtual directory you want to delete, and then call the AppDelete method.
Copy Code code as follows:

DirectoryEntry de = RootFolder. Children.find ("ASPCN", "IIsVirtualDir");
De. Invoke ("AppDelete", true);
Rootfolder.commitchanges ();

Another way is to invoke the Delete method of the root directory.
Copy Code code as follows:

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 ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.