Home>
using system;
using system.directoryservices;
using system.collections;
namespace iismanagement
{
///<summary>
///A summary description of iismanager.
///</summary>
public class iismanager
{
//define what needs to be used
private string _server, _website;
private virtualdirectories _virdirs;
protected system.directoryservices.directoryentry rootfolder;
private bool _batchflag;
public iismanager ()
{
//Use localhost by default, that is, access the local machine
_server="localhost";
_website="1";
_batchflag=false;
}
public iismanager (string strserver)
{
_server=strserver;
_website="1";
_batchflag=false;
}
///<summary>
///define public properties
///</summary>
//server attribute defines the name of the accessing machine,Can be ip and calculated name
public string server
{
get {return _server;}
set {_server=value;}
}
//website attribute definition,Is a number,For convenience, use string
//Generally speaking, the first host is 1, the second host is 2, and so on.
public string website
{
get {return _website;}
set {_website=value;}
}
//The name of the virtual directory
public virtualdirectories virdirs
{
get {return _virdirs;}
set {_virdirs=value;}
}
///<summary>
///Define public methods
///</summary>
//connect to the server
public void connect ()
{
connecttoserver ();
}
//for convenience
public void connect (string strserver)
{
_server=strserver;
connecttoserver ();
}
//for convenience
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
{
//Add to the children collection of root
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.");
}
}
//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 not 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 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 the operation is a virtual directory
paras [1]=strvirdir;
rootfolder.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 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);
}
}
//Perform 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 ();
}
//Get the virtual directory collection
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, _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;_authntlm=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 has too many attributes
///I have only done some of the more important ones,Everyone else needs to add it.
///</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 _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;}
}
}
///<summary>
///collection virtualdirectories
///</summary>
public class virtualdirectories:system.collections.hashtable
{
public virtualdirectories ()
{
}
//Add a new method
public virtualdirectory find (string strname)
{
return (virtualdirectory) this [strname];
}
}
}
c
  • Previous Implementation of the famous Schwartzian transformation problem in Perl
  • Next Python statistical CPU usage method