How to operate IIS in .NET

xiaoxiao2021-03-06  107

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

How to operate IIS (Source Code) in .NEN (Source Code) Using System; Using System.data; Using System.directoryServices; Using 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;} ///

// // define the 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;}} // The Website property is defined, for a number, for convenience, using string // Generally, the first host is 1, the second host is 2, and push the public string Website {get {Return _Website;} set {_Website = value; }}} // Virtual directory name public virtualdirector virtirs {get {return_virdirs;} set {_virdirs = value;} } /// /// Define Public Method /// // Connection Server Public Void Connect () {ConnectTOSERVER ();} // For convenience of overloading public void connect (String string) {_server = strServer; ConnectToServer ();} // overloaded to facilitate public void Connect (string strServer, 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 (! _ Virdirs. Contains (newdir.Name) || _batchflag) {try {// added to the ROOT Children set 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 NOTS";} Return TMP;} // Update a virtual directory public void update (VirtualDirectory DIR) {// Deconstimate whether the virtual directory that needs to be changed does IF (_VIRDIRS.CONTAINS (Dir.name) ) {DirectoryEntry ode = rootfolder.Children.Find (dir.Name, "IIsWebVirtualDir"; UpdateDirInfo (ode, dir);} else {throw new Exception ( "This virtual directory is not exists.";}} // remove a virtual Directory public void delete (String strircir) {if (_Virdirs.contains (strvirDir)) {Object [] Paras = new object [2]; Paras [0] = "

IisWebVirtualDir "; // means operation is the virtual directory Paras [1] = STRVIRDIR; rootfolder.invoke (" delete ", paras); rootfolder.commitchanges ();} else {throw new exception (" can't delete " strVirdir ", BECAUSE ISN't EXISTS.";}} // Batch update public void updatebatch () {BatchUpdate (_VIRDIRS);} // Reserved one :-) public void updatebatch (VirtualDirectories VDS) {BatchUpdate (VDS) } ///

/// Private method /// // Connection server private void connectionTOSERVER () {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 not connect to the server [" _Server "] ...", e);}} // Execute 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 ["enabledefaultdoc"] [0] = vd.enableDefaultdoc; de.properties [ EnabledirBrowsing "] [0] = vd.enableDirBrowsing; de.properties [" accessssl "] [0] = vd.accesssssl; de.properties [" AccessScript "] [0] = vd.accesssScript; 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.Prop ERTIES [AccessRead "] [0]; vd.accessexecute = (bool) de.properties [" accessexecute "] [0]; vd.accessWrite = (bool) de.properties [" accesswrite "] [0]; VD. AnonymousUserName = (string) de.Properties [ "AnonymousUserName"] [0]; vd.AnonymousUserPass = (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.accessssssl = (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, _authbasic, _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; _endirbrow = 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.

/// public int flag {get {return _flag;} set {_flag = value;}} public bool accessread {get {return _read;} ​​set {_read = value;}} public bool accesswrite {Return _write;} set {_write = value;}} public bool AccessExecute {get {return _execute;} set {_execute = value;}} public bool AccessSSL {get {return _ssl;} set {_ssl = value;}} public bool Accessscript {get {return _script;} set {_script = value;}} public bool AuthBasic {get {return _authbasic;} set {_authbasic = value;}} public bool AuthNTLM {get {return _authntlm;} set {_authntlm = value;} } Public bool contentIndexed {get {return _indexed;} set {_indexed = value;}} public bool enabledirbrowsing {get {return_endirbrow;} set {_endirbrow = value;}} public bool EnableDefaultDoc {get {return _endefaultdoc;} set {_endefaultdoc = value;}} public string Name {get {return _name;} set {_name = value;}} public string Path {get {return _path;} set {_path = value;}} public string DefaultDoc {get {return _defaultdoc;} set {_defaultdoc = value;}} public string AnonymousUserName {get {return _ausername;} set {_ausername = value;}} public string AnonymousUserPass {get {return _auserpass;} Set {_auserpass = value;}}} ///

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

New Post(0)