. NET how to operate IIS

Source: Internet
Author: User

. NET how to operate IIS

NET has actually done a good job for us in this area. The FCL provides a number of classes to help us do this work, making our development work very simple and enjoyable. Programmatic control of IIS is actually quite simple, as with ASP. NET requires the use of ADSI to operate IIS, but at this point we no longer need to getobject this stuff, because. NET provides us with a new and more powerful feature.
The System.DirectoryServices namespace includes powerful Dongdong--directoryentry,directoryentries that provide us with the power to access Active Directory, where we operate IIS, LDAP , NDS, and Winnt, it's very powerful:)

But we're talking about IIS control here, in general, we operate on IIS generally as a virtual directory, so I'll take this as the main content.

First of all we need to figure out the hierarchy of IIS, and here's a diagram from abroad that explains the hierarchy of IIS:

[Htmchina:image id=image1|12] [/htmchina:image]
In order to understand the control syntax of IIS, we have to be aware of the hierarchy of IIS metadata (Metabase). Each node in the graph is called a key, and each key can contain one or more values, which are the properties that we say, and the keys in the IIS metadata match the elements in IIS, so the setting of the property values in the metadata affects the settings in IIS. This is the basic idea and core of our programming.

Also look at the concept of schema. It represents the name of the schema in IIS, which is the type of key that can be understood in IIS metadata, specifically the type of each node. We know that there are virtual directories, plain directories, and files in IIS, and these are all elements of IIS, and their logo is the schema. For example, the schema of the virtual directory is "IIsVirtualDir", the normal directory is "Iiswebdir". So when we add and remove directories, IIS knows whether 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 entry. When people who have used ADSI know that the operation Iis,winnt These, we also need to provide their path, when operating IIS, the path is in the format:

Iis://computername/service/website/directory

ComputerName: The name of the server to be operated on, either the name or the IP, often using localhost
Service: The operation of the server, IIS has the Web, there are FTP, and SMTP these services, we mainly operate IIS Web features, so here is "w3svc", if it is ftp should be "MSFTPSVC"
WebSite: An IIS service can include a number of sites, which are used to set the operations of the site. His value is a number, default is 1, indicates the default site, if there are other, then starting from 1 and so on.
Directory: Needless to say, the directory name of the operation, a site generally the top-level directory is "ROOT", the other directory is his children (child).
First we get the top level directory of a site (root directory):

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, for example, we're adding "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, a subset of the root directory (RootFolder. Children) Add another record, which is used in the Directoryentries class of the Add method, which returns a DirectoryEntry, representing 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 joining. Then using the DirectoryEntry invoke method, invoke the "AppCreate" method in ADSI to actually create the directory (it does not seem to take this step to create a directory success, but for the sake of insurance, everyone still use it), and then in turn calls the new, The root directory of the CommitChanges method, confirm this operation.

When creating a new directory, we can also assign values to the properties of this directory at the same time, but my actual combat experience tells me that it is best not to do so, if the creation of the assignment, there will be many properties can not be assigned success, such as important to represent the real directory of the Path property. Therefore, it is recommended that you create the directory first, and then assign values, that is, update the directory information.

Update virtual Directory

I believe 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. The assignment can be done in two ways:

The first is to invoke 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 of these methods are feasible. Depends on your preference.

Before assigning a value, we need to determine the target to be assigned:) Here we use the Find method of the Directoryentries class, such as:

directoryentry de = root

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.