To create a virtual directory in IIS with asp.net

Source: Internet
Author: User

One. First reference in the project

System.DirectoryServices.dll

Using System.DirectoryServices;
protected System.DirectoryServices.DirectoryEntry dirroot;

1, add a new virtual directory

DirectoryEntry Newvirdir = Dirroot. Children.add ("WebTest", "IIsWebVirtualDir");
Newvirdir.invoke ("AppCreate", true);
Newvirdir.commitchanges ();
Dirroot.commitchanges ();

2. Change Virtual directory Properties

The properties of virtual directories are more commonly used: Accessread,accesswrite,accessexecute,accessscript,defaultdoc,enabledefaultdoc,path, etc.

DirectoryEntry Dirport = Dirroot. Children.find ("WebTest", "IIsVirtualDir");
Dirport. properties["AccessRead"][0] = true;

3. Delete virtual directory

DirectoryEntry Dirport = Dirroot. Children.find ("WebTest", "IIsVirtualDir");
Dirport.invoke ("AppDelete", true);
Dirroot.commitchanges ();

Or:

Object[] part = new OBJECT[2];
Part[0] = "IIsWebVirtualDir";
PART[1] = "webtest";
Dirroot. Invoke ("Delete", part);
Dirroot.commitchanges ();

In fact, if you follow the steps above, you can neither understand what it means, or make a mistake. Let's take a serious look at the following.

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.

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:

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

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

If we find it, we can assign a value. You must take a good look at the assignment, the virtual directory can be more than the attribute value, a lot of checks. : (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.

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.

Object[] paras = new object[2];
Paras[0] = "IIsWebVirtualDir"; Represents the virtual directory of the operation
PARAS[1] = "ASPCN";
RootFolder. Invoke ("Delete", paras);
Rootfolder.commitchanges ();

Related Article

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.