How to operate IIS in. net)

Source: Internet
Author: User
. 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, it should be "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.

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 a 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.

Object [] paras = new object [2];
Paras [0] = "iiswebvirtualdir"; // indicates that a virtual directory is operated.
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.