How to operate IIS in .NET (Source Code)

xiaoxiao2021-03-06  43

.Net how to operate IIS (source code) Author: Flying www.ASPCool.com Time: 2002-6-9 15:21:55 Views: 6619

/// ********************************************************* ************ /// ************** IIS control management class 1.0 beta ************* / // ************* Author: flying knife ************* / /// ************* * http://www.aspcn.com ************** / // ************************ FEIDAO@aspcn.com **** ********** /// ************** 2002.05.25 6 days before World Cup ************* // / ************************************************** **************; use system.directoryServices; use system.collection; 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 virtualdirectories virdirs {get {_virdirs = value;}} /// /// definition public method /// // Connection server PUBLIC Void connection () {connectionToServer ();} / / To facilitate overloaded public void Connect (string strServer) {_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"; newvirdir.commitchanges (); rootfolder.commitchanges (); // then update data UpdatedirInfo (newvirdir, newdir);} catch (exception EE) {throw new exception;} } 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 = _VIRDIRDIR); (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) {// Determines if the virtual directory that needs to be changed does IF (_VIRDIRDIRS.CONTAINS (Dir.Name)) {DirectoryEntry Ode = rootfolder.ch Ildren.Find (Dir.Name, "IisWebVirtualDir); UpdatedirInfo (ODE, DIR);} else {throw new exception (" this Virtual Directory is not exists (}} // Delete a virtual directory public void delete String strircir) {if (_virdirs.contains (strircir)) {Object [] Paras = new object [2]; Paras [0] = "IisWebVirtualDir"; // means operation is the virtual directory Paras [1] = STRVIRDIR; rootfolder .Invoke ("delete"; rootfolder.commitchanges ();} else {throw new Exception ("Can't delete" strvirdir ", Because it isn't exists.");}} // Batch update Public void updatebatch () {BatchUpdate (_VIRDIRS);

} // Overload one :-) public void updatebatch (VirtualDirectories vds) {BatchUpdate (VDS);} ///

/// Private method /// // Connect 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);}} // 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 [ "AnonymousU Serpass "] [0] = vd.anonymoususeuserpass; 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.accessssl; 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.properties ["accessread"] [0]; vd.accessexecute = (bool) DE. Properties ["accessexecute"] [0]; vd.accessWrite = (bool) de.properties ["accesswrite"] [0]; vd.anonymoususeusername = (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 = "";} /// /// definition attribute, iisvirtualdir too many attributes /// I only have some important, and 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;}}} ///

// / Collection VirtualDirectories ///

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

New Post(0)