How to operate IIS in .NET

xiaoxiao2021-03-06  57

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. When you operate IIS, this path format is: IIS: // computername / service / Website / Directory Computename: That's a server Name, can be named or IP, often used by localhost service: the server, IIS has web, there is FTP, and SMTP these services, we mainly operate IIS's web feature, so "here is" W3SVC, if FTP should be "MSFTPSVC" Website: A lot of sites can be included in an IIS service, which is used to set up a 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. Commitchange (); What kind of program habits are read :)) How to operate IIS (source code) in. "Using system.data; useing system.directoryservices; use system.collections; namespace aspcn.management {/ //

/// iiSManager's summary description.

/// public class IISManager {private string _server // need to use defined, _website; private VirtualDirectories _virdirs; protected System.DirectoryServices.DirectoryEntry rootfolder; private bool _batchflag; public IISManager () {// Use default LocalHost, that is, accessed the local machine_Server = "localhost"; _Website = "1"; _batchflag = false;} public iismanager (String strserver) {_server = strserver; _Website = "1"; _BATCHFLAG = false;} /// < Summary> // / Define public property /// // Server property Defines the name of the access machine, which can be IP and calculation name public string server {get {returver;} set {_server = value;}} / / Website property definition, for a number, for convenience, using string // Generally, the first host is 1, the second host is 2, and push public string Website {get {return _Website;} set {_Website = value }}} // Virtual directory name public virtualdirectories virdirs {get {return_virdirs;} set {_virdirs = value;}} ///

/// definition public method /// // connection server Public void connection () {ConnectToServer ();} // For easy to overload public void connect (String strserver) {_server = strser ();} // for easy heavy duty PUBLIC VOID CONNECT (String S trServer, string strWebSite) {_server = strServer; _website = strWebSite; ConnectToServer ();} // determines whether the virtual storage directory public bool Exists (string strVirdir) {return _virdirs.Contains (strVirdir);} // add a virtual directory Public void create (VirtualDirectory newdir) {string strpath = "IIS: //" _Server "/ w3svc /" _Website "/ root /" newdir.name; if (! _ vivontirs.contains (newdir.name) | | _BATCHFLAG) {Try {// Add to root in the Children collection to DirectoryEntry newvirdiR = rootfolder.children.add (NewDir.Name, "IisWebVirtualDir"; newvirdir.invoke ("AppCreate", true);

newVirDir.CommitChanges (); rootfolder.CommitChanges (); // then the update data UpdateDirInfo (newVirDir, newdir);} catch (Exception ee) {throw new Exception (ee.ToString ());}} else {throw new Exception ( "This virtual directory is already exist.";}} // get a virtual directory public VirtualDirectory GetVirDir (string strVirdir) {VirtualDirectory tmp = null; if (_virdirs.Contains (strVirdir)) {tmp = _virdirs.Find (strVirdir); ((VirtualDirectory) _virdirs [strVirdir]) flag = 2;.} else {throw new Exception ( "This virtual directory is not exists";} return tmp;} // update a virtual directory public void update (VirtualDirectory dir) {/ / Judgment whether the virtual directory that needs to be changed exists if (_virdirs.contains (dir.name)) {DirectoryEntry Ode = rootfolder.children.find (Dir.Name, "IisWebVirtualdir"; UpdatedirInfo (Ode, DIR);} else {throw new Exception ("This Virtual Directory IS NOTS.";}} // Remove a virtual directory public void delete (_virdirs.contains (strvirdir)) {Object [] Paras = new object [2]; Paras [0] = "IisWebVirtualDir"; // Indicates the operation is the virtual directory Paras [1] = STRVIRDIR; ROOT Folder.invoke ("delete"; rootfolder.commitchanges ();} else {throw new Exception ("Can't delete" strvirdir ", Because it isn't exists,";}} // Batch update Public void updatebatch () {batchupdate;} // Reserved one :-) public void updatebatch (VirtualDirectories) {BatchUpdate (VDS);} ///

/// Private method /// // Connection Server Private Void ConnectToServer () {string strpath = "IIS: //" _server "/ w3svc /" _Website "/ root"; try {this.rootfolder = new DirectoryEntry (strpath); _Virdirs = GetVirdirs (this.rootfolder.children);

} Catch (Exception E) {Throw new Exception ("Can't Connect To The Server [" _Server "] ...", E);}} // Perform Batch Update Private Void BatchUpdate (VirtualDirectories VDS) {_batchflag = True; Foreach (Object Item in vds.values) {VirtualDirectory VD = (VirtualDirectory) Item; Switch (vd.flag) {Case 0: Break; Case 1: Create (VD); Break; Case 2: Update (VD) ; break;}} _batchflag = false;} // update stuff private void UpdateDirInfo (DirectoryEntry de, VirtualDirectory vd) {de.Properties [ "AnonymousUserName"] [0] = vd.AnonymousUserName; de.Properties [ "AnonymousUserPass"] [0] = vd.anonymoususerpass; de.properties ["accessread"] [0] = vd.accessread; de.properties ["accessexecute"] [0] = vd.accessExecute; de.properties ["accesswrite"] [0 ] = vd.accessWrite; de.properties ["authbasic"] [0] = vd.authbasic; de.properties ["authnTlm"] [0] = vd.authnTlm; de.properties ["contentIndexed"] [0] = Vd.contentIndexed; de.properties ["enablededefaultdoc"] [0] = vd.enableDefaultdoc; de.properties ["enabledirbrowsing"] [0] = vd.enableDirBrowsing; de.pr Operties ["Accessssl"] [0] = vd.accesssssl; de.properties ["AccessScript"] [0] = vd.accessscript; de.properties ["defaultdoc"] [0] = vd.defaultdoc; de.properties [ "Path"] [0] = vd.Path; de.CommitChanges ();} // set the virtual directory acquired private VirtualDirectories GetVirDirs (DirectoryEntries des) {VirtualDirectories tmpdirs = new VirtualDirectories (); foreach (DirectoryEntry de in des) {if (de.schemaclassname == "iiswebvirtualdir" {VirtualDirectory vd = new virtualdirectory (); vd.name = de.name; vd.accessRead =

(BOOL) de.properties ["accessread"] [0]; vd.accessexecute = (bool) de.properties ["accessexecute"] [0]; vd.accessWrite = (BOOL) de.properties ["accesswrite"] [ 0]; vd.anonymoususername = (string) de.properties ["anonymoususername"] [0]; vd.anonymoususeuserpass = (string) de.properties ["anonymoususername"] [0]; vd.authbasic = (bool) DE. Properties ["Authbasic"] [0]; vd.authnTLM = (bool) de.properties ["AuthnTLM"] [0]; vd.contentIndexed = (bool) de.properties ["contentIndexed"] [0]; vd. EnableDefaultDoc = (bool) de.Properties [ "EnableDefaultDoc"] [0]; vd.EnableDirBrowsing = (bool) de.Properties [ "EnableDirBrowsing"] [0]; vd.AccessSSL = (bool) de.Properties [ "AccessSSL" ] [0]; vd.accessscript = (bool) de.properties ["AccessScript"] [0]; vd.path = (string) de.properties ["path"] [0]; vd.flag = 0; VD .Defaultdoc = (string) de.properties ["defaultdoc"] [0]; tmpdirs.add (vd.name, vd);}} Return TmpDirs;}} ///

/// VirtualDirectory class /// public class virtualdirectory {private bool _read, _execute, _script, _ssl, _write, _auth basic, _authntlm, _indexed, _endirbrow, _endefaultdoc; private string _ausername, _auserpass, _name, _path; private int _flag; private string _defaultdoc; /// /// Constructor /// public VirtualDirectory () {SetValue ();} public VirtualDirectory (string strVirDirName) {_name = strVirDirName; SetValue ();} private void SetValue () {_read = true; _execute = false; _script = false; _ssl = false; _write = false; _authbasic = False; _AUTHNTLM = false; _Indexed = false; _endefaultdoc = false; _flag =

1; _defaultdoc = "default.htm, default.aspx, default.asp, index.htm"; _path = "c: //"; _AUSERNAME = "; _AUSERPASS ="; _ name = "";} ///

/// Define attributes, IISVirtualdir too many attributes /// I only have more important, other big guys need to add it.

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

New Post(0)