IIS management class (C #)

Source: Internet
Author: User
///************************************* **********************

/// ************** IIS control management class 1.0 Beta **************

/// ************** Author: Flying knife **************

/// ************** Http://www.ASPcn.com **************

/// ************** Feidao@ASPcn.com **************

///************************************* **********************

Using system;

Using system. Data;

Using system. directoryservices;

Using system. collections;

Namespace aspcn. Management

{

/// <Summary>

/// Summary of iismanager.

/// </Summary>

Public class iismanager

{

// Define

Private string _ server, _ website;

Private virtualdirectories _ virdirs;

Protected system. directoryservices. directoryentry rootfolder;

Private bool _ batchflag;

Public iismanager ()

{

// By default, localhost is used to access the local machine.

_ Server = "localhost ";

_ Website = "1 ";

_ Batchflag = false;

}

Public iismanager (string strserver)

{

_ Server = strserver;

_ Website = "1 ";

_ Batchflag = false;

}

/// <Summary>

/// Define public attributes

/// </Summary>

// Server attribute defines the name of the access machine, which can be IP address and computing name

Public String Server

{

Get {return _ server ;}

Set {_ Server = value ;}

}

// Defines the website attribute as a number. For convenience, use string

// Generally, the first host is 1, the second host is 2, and so on.

Public String website

{

Get {return _ website ;}

Set {_ website = value ;}

}

// Virtual directory name

Public virtualdirectories virdirs

{

Get {return _ virdirs ;}

Set {_ virdirs = value ;}

}

/// <Summary>

/// Define public methods

/// </Summary>

// Connect to the server

Public void connect ()

{

Connecttoserver ();

}

// For convenience of overloading

Public void connect (string strserver)

{

_ Server = strserver;

Connecttoserver ();

}

// For convenience of overloading

Public void connect (string strserver, string strwebsite)

{

_ Server = strserver;

_ Website = strwebsite;

Connecttoserver ();

}

// Determine whether the virtual directory is saved

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

{

// Add it to the children set of the root user

Directoryentry newvirdir = rootfolder. Children. Add (newdir. Name, "iiswebvirtualdir ");

Newvirdir. Invoke ("appcreate", true );

Newvirdir. commitchanges ();

Rootfolder. commitchanges ();

// Then update the data

Updatedirinfo (newvirdir, newdir );

}

Catch (exception ee)

{

Throw new exception (EE. tostring ());

}

}

Else

{

Throw new exception ("this virtual directory is already exist .");

}

}

// Obtain 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)

{

// Determine whether the virtual directory 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 not exists .");

}

}

// Delete a virtual directory

Public void Delete (string strvirdir)

{

If (_ virdirs. Contains (strvirdir ))

{

Object [] paras = new object [2];

Paras [0] = "iiswebvirtualdir"; // indicates that a virtual directory is operated.

Paras [1] = strvirdir;

Rootfolder. Invoke ("delete", paras );

Rootfolder. commitchanges ();

}

Else

{

Throw new exception ("can't delete" + strvirdir + ", because it isn' t exists .");

}

}

// Batch update

Public void updatebatch ()

{

Batchupdate (_ virdirs );

}

// Reload one :-)

Public void updatebatch (virtualdirectories VDS)

{

Batchupdate (VDS );

}

/// <Summary>

/// Private Method

/// </Summary>

// Connect to the 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 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 ();

}

// Obtain the virtual directory set

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. 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, _ endefadoc Doc;

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; _ authntlm = false;

_ Indexed = false; _ endirbrow = false; _ endefadoc Doc = false;

_ Flag = 1;

_ Defaultdoc = "default.htm,default.aspx,default.asp,index.htm ";

_ Path = "C :\\";

_ Ausername = ""; _ auserpass = ""; _ name = "";

}

/// <Summary>

/// Define attributes. iisvirtualdir has too many attributes.

/// I only did some important work. Other people need to add their own products.

/// </Summary>

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 _ endefadoc Doc ;}

Set {_ endefadoc Doc = value ;}

}

Public string name

{

Get {return _ name ;}

Set {_ name = value ;}

}

Public String path

{

Get {return _ path ;}

Set {_ Path = value ;}

}

Public String defaultdoc

{

Get {return _ defadoc Doc ;}

Set {_ defadoc Doc = value ;}

}

Public String anonymoususername

{

Get {return _ ausername ;}

Set {_ ausername = value ;}

}

Public String anonymoususerpass

{

Get {return _ auserpass ;}

Set {_ auserpass = value ;}

}

}

/// <Summary>

/// Set virtualdirectories

/// </Summary>

Public class virtualdirectories: system. Collections. hashtable

{

Public virtualdirectories ()

{

}

// Add a new method

Public virtualdirectory find (string strname)

{

Return (virtualdirectory) This [strname];

}

}

}

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.