How to operate IIS in .NET

zhaozj2021-02-16  93

Net actually has been doing well in this regard. Among the FCL provides a lot of classes to help us complete this work and make our development work very simple and happy. Programming Control IIS is actually very simple, like ASP, and ADSI needs to use ADSI to operate IIS, but we will no longer need GetObject this stuff, because .NET provides us with a more powerful new Stuff. System.directoryServices Namespace includes some powerful stuff-DirectoryEntry, DirectoryEntries, which provides us with powerful features for accessing the active directory, allowing us to operate IIS, LDAP, NDS, and Winnt, which is very powerful. :) But we only talk about IIS control, in general, our operation IIS is generally the operation of the virtual directory, so I will list this as the main content. First of all, we must figure out the problem of IIS's hierarchical structure. Here is a picture I found abroad. It is very good to explain the hierarchy of IIS: [htmchina: image id = image1 | 12] [/ htmchina: image] In order to figure out the syntax of the IIS, we must understand the above chart to understand the hierarchy of the Metabase. Each of the figures is called KEY, and each Key can contain one or more values, which is the property we say, the key in the IIS metadata is consistent with the elements in IIS, so The setting of the attribute value in the data affects the settings in IIS. This is the basic idea and core of our programming. Also have a look at the concept of Schema. It represents the name of the architecture in the IIS, you can understand the type of Key in the IIS metadata, specifically, means the type of each node. We know that there is a virtual directory, ordinary directory, and files in the IIS, which belongs to the elements of IIS, distinguishing their flags are Schema. For example, the schema of the virtual directory is "Iisvirtualdir", the ordinary directory is "Iiswebdir". This way we add and delete the directory, IIS know that we add a virtual directory or a normal directory. Creating a virtual directory DirectoryEntry is a big gift for us. His name We know his function - the entrance of the catalog. People who have used ADSI know that when operating IIS, Winnt, we also need to provide their path, this PATH format is: IIS: // computername / service / Website / Directory

ComputerName: The name of the server, can be named or IP, often used by LocalHost Service: The server, IIS has a web, there is FTP, and SMTP these services, we are mainly Web for operation IIS Function, therefore, "W3SVC", if FTP should be "MSFTPSVC" Website: A number of sites can be included in an IIS service, which is used to set the site. His value is a number, default is 1, indicating the default site, if there is anything, then push from 1. Directory: Don't say, the directory name of the operation, a site general top-level directory is "root", and other directory is his child (Child). First we get a top-level directory (root directory): DirectoryEntry Rootfolder = New DirectoryEntry ("IIS: // LocalHost / W3SVC / 1 / root"); if we create this object, it is true that this directory is true. existing. Let's add a new virtual directory, such as what we have to add "ASPCN": DirectoryEntry newvirdir = rootfolder.children.add ("ASPCN", "IisWebVirtualDir"); NewVirdir.Invoke ("AppCreate", true); newvirdir. Commitfolder.commitchanges (); Create a directory's ideas, that is, add a record in the subset of the root directory (rootfolder.children), use the add method in the DirectoryEntries class, which is returned. A DirectoryEntry, indicating the newly joined directory, the first parameter is the name of the virtual directory, the second is the class name of Schema to indicate the type of directory we join. Then use DirectoryEntry's Invoke method, call the "AppCreate" method in ADSI to truly create (seek not going this step can also create a directory success, but for insurance, everyone still uses it), and finally call new, The COMMITCHANGES method of the root directory confirms this operation. When you create a new directory, we can also assign a value to this directory at the same time, but my actual experience tells me that it is best not to do this. If you create it, you will have a lot of attributes that you can't assign a value, such as important representation. The PATH property of the directory. So the flying knife suggests that everyone is best to create a directory, then assign the value, that is, update the directory information. Updating the virtual directory believes that everyone is more familiar with IIS, understands some important settings in IIS, such as readable (AccessWrite), executable (AccessExecute), etc. These can be implemented by assigning the DirectoryEntry's Properties attribute set.

Assignment can be completed in two ways: the first is the add method of calling the Properties collection, such as Dir.Properties ["AccessRead"]. Add (true); the second is to assign the first index value: DIR. Properties [AccessRead "] [0] = true; these two methods are feasible. Specifically, you have to look at your preference. We still have to determine the target to assign values ​​before making assignment :) Here we use the DirectoryEntries class Find method, such as: DirectoryEntry de = rootfolder.children.Find ("aspcn", "Iisvirtualdir); found, we It can be assigned. Be sure to look at it when you assign a value, the attribute value of the virtual directory can be super, and a lot is checked. . :( Too much, I don't repeat, everyone go to Microsoft's site :) Compare commonly: AccessRead, AccessWrite, AccessExecute, AccessScript, defaultdoc, enabledeDefaultdoc, Path Delete virtual directory delete virtual directory methods Very simple, that is to find the virtual directory you want to delete, then call the AppDelete method. DirectoryEntry de = rootfolder.children.Find ("ASPCN", "IisvirtualDir"); de.invoke ("appdelete"; rootfolder.commitchanges (); there is a way to call the root directory of the DELETE method. Object [] Paras = New Object [2]; Paras [0] = "IisWebVirtualDir"; // Indicates that the operation is the virtual directory Paras [1] = "aspcn"; rootfolder.invoke ("delete", paras); rootfolder. Commitchanges ();

Which one likes to read programming habits :))

转载请注明原文地址:https://www.9cbs.com/read-11856.html

New Post(0)