How C # operates the IIS root directory

Source: Internet
Author: User
Tags foreach iis root directory

The example in this article describes how C # operates the IIS root directory. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 The 96 97 98 99 100 101 102 103 104 105 106 107 108 109 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140-1 41 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170-171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201-2 02 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231-232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262-2 63 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298-299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329-3 30 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359-360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 /td> Using System; Using System.DirectoryServices; Using System.Collections; Summary description of namespace Iismanagement {///<summary>///iismanager. </summary> public class Iismanager {//define the private string _server,_website to be used; private virtualdirectories _vird IRs protected System.DirectoryServices.DirectoryEntry RootFolder; private bool _batchflag; Public Iismanager () {//localhost by default, that is, accessing the local machine _server = "localhost"; _website = "1"; _batchflag = false;} public Iisman Ager (string strserver) {_server = strserver; _website = "1"; _batchflag = false;}///<summary>///define public properties/// The;/summary>//server property defines the name of the access machine, which can be IP and compute name public string Server {get{return _server;} set{_server = value;}}//websit e property definition, for a number, for convenience, use string//generally the first host is 1, the second host is 2, and then the public string WebSite {get{return _website;} set{_website = value; }///virtual directory name public virtualdirectories virdirs {get{return _virdirs;} set{_virdirs = value;}///<summary>/// Defining public methods///</summary>//Connecting Serverspublic void Connect () {connecttoserver ()}///is convenient for overloading public void Connect (string strserver) {_server = strserver; Connecttoserver (); }//For convenience overload public void Connect (string strserver,string strwebsite) {_server = strserver; _website = Strwebsite; Connecttoserver (); //Determine whether to save this virtual 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 {//join to the Children collection of root to DirectoryEntry Newvirdir = RootFolder. Children.add (Newdir. Name, "IIsWebVirtualDir"); Newvirdir.invoke ("AppCreate", true); Newvirdir.commitchanges (); Rootfolder.commitchanges (); Then update the data updatedirinfo (NEWVIRDIR,NEWDIR); The 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 isn't exists");} return tmp; //Update a virtual directory public void update (VirtualDirectory dir) {//Determine if 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 isn't exists.");} Deletes a virtual directory public void Delete (string strvirdir) {if (_virdirs). Contains (Strvirdir)) {object[] paras = new object[2]; Paras[0] = "IIsWebVirtualDir";//means virtual directory paras[1] = Strvirdir; r Ootfolder. Invoke ("Delete", paras); Rootfolder.commitchanges (); else {throw new Exception ("Can" t Delete "+ Strvirdir +", because it isn ' t exists. ");} Bulk update public void UpdateBatch () {BatchUpdate (_virdirs);}//Overload a:-) public void UpdateBatch (Virtualdirectories vds) {Ba Tchupdate (VDS); ///<summary>///Private Method///</summary>//connection server private void Connecttoserver () {String strpath = "iis://" + _ser ver + "/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 bulk 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 dongdong 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. AccessSSL; De. properties["AccessScript"][0] = VD. AccessScript; De. properties["DefaultDoc"][0] = VD. DefaultDoc; De. properties["Path"][0] = VD. Path; De.commitchanges (); //Get virtual directory Collection private virtualdirectories getvirdirs (Directoryentries des) {virtualdirectories tmpdirs = new Virtualdirect Ories (); 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. 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; } <summary>///VirtualDirectory class///</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; <summary>///Constructor///</summary> 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;_authn Tlm=false; _indexed = False;_endirbrow=false;_endefaultdoc = false; _flag = 1; _defaultdoc = "default.htm,default.aspx,default.asp,index.htm"; _path = "C:"; _ausername = ""; _auserpass = ""; _name= ""; ///<summary>///define attributes, IIsVirtualDir too many properties///I only have a few more important, the other guys need to add it. </summary> public int Flag {get{return _flag.} set{_flag = value;} public bool AccessRead {get{return _rea D;} 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{_SC Ript = 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{R Eturn _endefaultdoc;} set{_endefaultdoc = value;}} public string Name {get{return _name} set{_name = value;} ' public string Path {get{return _path;} set{_path = Valu e;} 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;}} <summary>///Collection Virtualdirectories///</summary> public class Virtualdirectories: System.Collections.Hashtable {public virtualdirectories () {}///Add New method public VirtualDirectory find (string strName) {RET Urn (virtualdirectory) this[strname]; } } }

I hope this article will help you with your C # programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.