Using system.data; using system.directoryservices; using system.collections;
Namespace iismanage {/// /// IIS management class, you can create site, virtual directory, delete virtual directory, etc. /// summary> public class iismanager {private string _server, _website, _anonymoususeuseuseroususes, _anonymoususername; private VirtualDirectories_virdirs; protected system.directoryservices.directoryentry Rootfolder; private bool _batchflag;
#REGION Construction Function /// /// Constructor /// summary> public iiSManager () {// By default, localhost is used, ie accessed the local machine_server = "localhost"; _WEBSITE = "1" _Batchflag = false;}
/// /// Constructor /// summary> /// server param> public iismanager (string strserver) {_server = strserver; _Website = "1" _Batchflag = false;}
/// /// Constructor /// summary> /// server param> /// Site, each site For a second site, it is 2, and the "String string" {_Server = strserver; _Website = Website;} #endregion #region defines the public Attributes
/// /// Anonymous Access User /// summary> public string anonymoususeusername {get {return_anonymoususername;} set {_anonymoususeusername = value;}}}
/// /// Anonymous access User Password /// summary> public string anonymoususeuserpass {get {return_anonymoususerpass;} set {_anonymoususerpass = value;}}
/// /// server, can be IP or calculated name /// summary> public string server {get {return_server;} set {_server = value;}} /// // / Site, generally said that the first host is 1, the second host is 2, and then push /// summary> public int Website {get {return convert.Toint32 (_Website);} set {_Website = Convert. TOSTRING (VALUE);}}}} /// /// Virtual directory name /// summary> public virtualdirectories virdirs {get {ket_virdirs;} set {_virdirs = value;}} #ENDREGION
#Region definition public method /// /// Get anonymous access user username and password /// summare> public void get_anonymoususeuser () {_anonymoususeuser () {_anonymoususeuser () = "IUSR_DEVE-Server"; _anonymoususername = "IUSR_DEVE-SERVER" ; VirtualDirectory vDir; try {Hashtable myList = (Hashtable) _virdirs; IDictionaryEnumerator myEnumerator = myList.GetEnumerator (); while (myEnumerator.MoveNext ()) {vDir = (VirtualDirectory) myEnumerator.Value;! if (vDir.AnonymousUserName = "" ! && vDir.AnonymousUserPass = "") {_AnonymousUserName = vDir.AnonymousUserName; _AnonymousUserPass = vDir.AnonymousUserPass; break;}}} catch {_AnonymousUserPass = "IUSR_DEVE-SERVER"; _AnonymousUserName = "IUSR_DEVE-SERVER";}}
/// /// Connection Server /// summary> public void connection () {ConnectTOSERVER ();} /// /// connection server /// summary> /// server name param> public void connection "{_server = strser ();} /// /// connection server /// summary> / // server name param> /// Site, generally said that the first host is 1, the second host is 2, and push it according to the class Param> public void connect (string string strewebsite) {_server = strserver; _Website = strWebsite; connebstoserver ();
/// /// Add a site /// summary> /// The path of the site param> public void CreateWebsite (String Websitename, String Port, String Path) {Try {DirectoryEntry Root = New DirectoryEntry ("IIS: //" THIS_SERVER "/ W3SVC"); int sitei = 1; foreach (DirectoryEntry E in root.children) {if (e.schemaclassname == "IisWebserver" ) {Int id = Convert.Toint32 (E.NAME); if (id> = SiteID) {siteid = ID 1;} if (E.Properties ["Serverbindings"] [0] .tostring () == ": " Port ": ") {throw new exception is already occupied, please select another port!");}}} DirectoryEntry Site = (DirectoryEntry) Root.Invoke ("Create", "IisWebserver", SiteID; Site.Invoke ("Put", "Servercomment", Websitename; Site.Invoke ("Put", "Keytype", "IisWebserver"); Site.Invoke ("Put", "Serverbindings", ":" port " : "); Site.Invoke (" Put "," ServerState ", 2); Site.Invoke (" Put "," FrontPageWeb ", 1); Site.Invoke (" Put "," DefaultDoc "," Index.aspx "); Site.Invoke (" PUT "," SecureBindings " ": 443:"); Site.Invoke ("Put", "ServerAutostart", 1); Site.Invoke ("Put", "Serversize", 1); Site.Invoke ("setInfo"); DirectoryEntry Sitevdir = Site.children.Add ("root", "iiswebvirtualdir"); sitevdir.properties ["Appisolated"] [0] = 2; Sitevdir.Properties ["path"] [0] = path; Sitevdir.Properties ["
AccessFlags "] [0] = 513; Sitevdir.Properties [" FrontPageWeb "] [0] = 1; //sitevdir.properties["approot"][0] =" LM / W3SVC / " SiteID " / root "; Sitevdir.Properties ["AppFriendlyName"] [0] = "root"; Sitevdir.commitchanges (); site.commitchanges ();} catch (exception ex) {throw ex;}} /// // All sites /// summary> /// returns> public system.collections.hashtable gets () {try {DirectoryEntry root = new DirectoryEntry ("IIS: //" this._server "/ w3svc "); Hashtable Sites = new hashtable (); Foreach (DirectoryEntry E in root.children) {if (e.schemaclassname ==" iisWebserver ") {sits.add (e.name, e);}}}}} Catch (Exception EX) {throw ex;}}
/// /// Determines if this virtual directory //// summary> /// Virtual directory name param> /// returns> Public BOOL EXISTS (String Strvirdir) {Return_Virdirs.contains (STRVIRDIR);} /// // / Add a virtual directory /// / summary> /// virtual directory param> /// returns> Public Bool CreatevirtualDirectory (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); return true;} catch (Exception) {return false;}} else {return False;}} /// /// Get a virtual directory /// / summary> /// virtual directory name param> / // returns> 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 ("Virtual Directory" StrvirDir "does not exist! ");} Return TMP;
/// /// Update a virtual directory /// //mmary> /// virtual directory param> public void update (virtualdirectory dir) {// judgment need to be changed virtual directory exists if (_virdirs.Contains (dir.Name)) {DirectoryEntry ode = rootfolder.Children.Find (dir.Name, "IIsWebVirtualDir"); UpdateDirInfo (ode, dir);} else {System.Windows.Forms .MessageBox.show ("Virtual Directory" Dir.name "Does not exist!");}} /// /// Delete a virtual directory /// summary> /// Virtual directory name param> public void delete (string strvirdir) {f (_virdirs.contains (strircir)) {object [] Paras = new object [2]; paras [0] =" iiswebvirtualdir "; // Indicates that the operation is the virtual directory Paras [1] = STRVIRDIR; rootfolder.invoke ("delete", paras); rootfolder.commitchanges ();} else {system.windows.Forms.MESSAGEBOX.SHOW ("virtual directory" strvirdir " Do not exist! ");}} /// /// Batch update /// summary> public void updatebatch () {BatchUpdate (_VIRDIRS);} /// /// Batch update / // summary> /// Virtual Directory Collection param> public void Updatebatch (VirtualDirectories VDS) {BatchUpdate (VDS);
/// /// Gets the virtual directory set /// summary> /// returns> public VirtualDirectories GetVirtualDirdctories () {VirtualDirectories vds = GetVirDirs (this.rootfolder.Children); return vds } #Endregion
#Region Private method
/// /// Close the current object /// summary> public void close () {_virdirs.clear (); _VIRDIRS = NULL; rootfolder.dispose ();
} /// /// Connection Server /// summary> private void connectiontoserver () {string strpath = "IIS: //" _server "/ w3svc /" _Website "/ root"; try {this.rootfolder = new DirectoryEntry (strPath); _virdirs = GetVirDirs (this.rootfolder.Children);} catch (Exception) {System.Windows.Forms.MessageBox.Show ( "Could not connect to the server:" _ server); }}}} /// /// Execute Batch Update /// summary> /// Virtual Directory Collection param> 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: CreateVirtualDirectory (vd); break; case 2: Update (vd); break ;}} _Batchflag = false;} /// /// Update specified virtual directory /// ///// param> /// To perform an updated virtual directory param> private void updatedirInfo (DirectoryEntry de, VirtualDirectory VD) {de.properties ["Anonymou SUSERNAME "] [0] = vd.anonymoususername; de.properties [" anonymoususeuserpass "] [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.accessssssl; de.properties [AccessScript "] [ 0] = vd.accessScript; de.properties ["defaultdoc"] [0] = vd.defaultdoc; de.properties ["path"] [0] = vd.path; de.commitchanges ();
/// /// Get virtual directory set /// summary> /// param> /// returns> private virtualdirector 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.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.accesssssl = (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;} #ENDREGON} /// /// virtual directory entity /// summary>
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; #region // constructor / /// Constructor /// summary> public virtualdirectory () {setValue ();} /// /// constructor /// summary> /// Virtual Directory Name param> Public VirtualDirectory (STRING SVIRDIRNAME) {setValue (); _name = summary> ////// summary> /// < Param name = "svirdirName"> Virtual directory name param> /// physical path param> ///
Public override string toString () {return this._name;}
#region defined attribute public int flag {get {return _flag;} set {_flag = value;}} public bool AccessRead {get {return _read;} set {_read = value;}} public bool AccessWrite {get {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 anonymoususeuserpass {get {return _aurserpass;} set {_auserpass = value;}} #ENDREGON}