Using system.data; using system.directoryservices; using system.collections;
Namespace iismanage {///
/// IIS management class, you can implement a site, virtual directory, delete virtual directory, etc. ///
Public Class Iismanager
{
Private string _server, _website, _anonymoususeuserpass, _anonymoususername;
Private VirtualDirectories_virdirs;
Protected system.directoryServices.directoryEntry Rootfolder;
Private bool _batchflag;
#REGION constructor //////
/// Constructor ///
Public IISMANAGER ()
{
// By default, Localhost is used, that is, access the local machine.
_server = "localhost";
_Website = "1";
_BATCHFLAG = FALSE;
}
///
/// Constructor ///
///
server
Public IISMANAGER (STRING STRSERVER)
{
_server = strserver;
_Website = "1";
_BATCHFLAG = FALSE;
}
///
/// Constructor ///
///
server
///
Site, each site is 1, the second site is 2, according to this class
Public IISMANAGER (String strserver, int Website)
{
_server = strserver;
_Website = Website.toStrument ();
_BATCHFLAG = FALSE;
}
#ndregion
#Region defines public properties
///
/// Anonymous Access User ///
Public String AnonymousUsername
{
Get {return_Anonymoususername;
Set {_anonymoususername = value;
}
///
/// Anonymous access user password ///
Public String AnonymousUserpass
{
Get {return_Anonymoususerpass;}
Set {_anonymoususeuserpass = value;
}
///
/// server, can be IP or calculated name ///
Public String Server
{
Get {return _server;}
Set {_server = value;}
}
///
/// site, generally said that the first host is 1, the second host is 2, and it is pushed ///
Public Int Website
{
Get {return convert.Toint32 (_Website);
Set {_Website = Convert.TOString (value);
}
///
/// The name of the virtual directory ///
Public VirtualDirectories Virdirs
{
Get {return _virdirs;
Set {_virdirs = value;}
}
#ndregion
#region Definition Public Method ///
/// Get anonymous access to the user name and password ///
Public void get_anonymoususeuser ()
{
_AnonymousUserpass = "IUSR_DEVE-Server";
_Anonymoususername = "IUSR_DEVE-Server";
VirtualDirectory vdir;
Try
{
Hashtable mylist = (havehtable) _virdirs;
IDictionaryenumerator myenumerator = myList.GeteNumerator ();
While (myenumerator.movenext ())
{
VDIR = (VirtualDirectory) myenumerator.value;
IF (vdir.anonymoususeusername! = "" && vdir.anonymoususeuserpass! = ")
{
_Anonymoususername = vdir.anonymoususeusername;
_AnonymousUserpass = vdir.anonymoususeuser;
Break;
}
}
}
Catch
{
_AnonymousUserpass = "IUSR_DEVE-Server";
_Anonymoususername = "IUSR_DEVE-Server";
}
}
///
/// connect to the server ///
Public void connect ()
{
ConnectTOSERVER ();
}
///
/// connect to the server ///
///
server nickname
Public Void Connect (String STRSERVER)
{
_server = strserver;
ConnectTOSERVER ();
}
///
/// connect to the server ///
///
server nickname
///
Site, generally said that the first host is 1, the second host is 2, and push it accordingly
Public Void Connect (String Strserver, String StrWebsite)
{
_server = strserver;
_Website = StrWebsite;
ConnectTOSERVER ();
}
///
/// Add a site ///
///
Path of the site
Public void CreateWebsite (String Websitename, String Port, String Path)
{
Try
{
DirectoryEntry Root = New DirectoryEntry ("IIS: //" THIS_SERVER "/ W3SVC");
INT SiteId = 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 (Port "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;
}
}
///
/// Get all sites ///
///
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")
{
Sites.Add (E.NAME, E);
}
}
Return Sites;
}
Catch (Exception EX)
{
Throw EX;
}
}
/ / / / / Judge whether to save this virtual directory ///
///
Virtual directory name
///
Public Bool EXISTS (String Strvirdir)
{
Return_Virdirs.contains (STRVIRDIR);
}
///
/// Add a virtual directory ///
///
virtical list
///
Public Bool CreatevirtualDirectory (VirtualDirectory Newdir)
{
String strpath = "IIS: //" _server "/ w3svc /" _Website "/ root /" newdir.name
IF (! _ virdirs.contains (newdir.name) || _batchflag
{
Try
{
// Add to the root of Children collection
DirectoryEntry newvirdir = rootfolder.children.add (newdir.name, "iiswebvirtualdir);
NewVirdir.Invoke ("AppCreate", TRUE;
Newvirdir.commitchanges ();
Rootfolder.commitchanges ();
/ / Then update the data
UpdatedirInfo (NewVirdir, NewDir);
Return True;
}
Catch (Exception)
{
Return False;
}
}
Else
{
Return False;
}
}
///
// get a virtual directory ///
///
Virtual directory name
///
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 ///
///
virtical list
Public Void Update (VirtualDirectory DIR)
{
// Determine if the virtual directory that needs to be changed
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 ///
///
Virtual directory name
Public void delete (String Strvirdir)
{
IF (_VIRDIRS.CONTAINS (STRVIRDIR))
{
Object [] Paras = New Object [2];
Paras [0] = "IisWebVirtualDir"; // means operation is a virtual directory
Paras [1] = STRVIRDIR;
Rootfolder.invoke ("delete", paras); rootfolder.commitchanges ();
}
Else
{
System.Windows.Forms.MessageBox.show ("Virtual Directory" StrvirDir "Does not exist!");
}
}
///
/// Batch update ///
Public void updatebatch ()
{
BatchUpdate (_VirdiR);
}
///
/// Batch update ///
///
Virtual directory collection
Public void Updatebatch (VirtualDirectories VDS)
{
BatchUpdate (VDS);
}
///
/// Get a virtual directory collection ///
///
Public VirtualDirectories getVirtualDIRDIRECTORIES ()
{
VirtualDirectories vds = getVirdirs (this.rootfolder.children);
Return VDS;
}
#ndregion
#Region Private method
///
/// Close the current object ///
Public void close ()
{
_VIRDIRS.CLEAR ();
_virdirs = null;
Rootfolder.dispose ();
} ///
/// connect to the server ///
Private void connectiontockerver ()
{
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 ("Unable to connect to the server:" _ server);
}
}
///
/// Execute bulk update ///
///
Virtual directory collection
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 the specified virtual directory ///
///
///
To perform an updated virtual directory
Private void UpdatedirInfo (DirectoryEntry de, VirtualDirectory VD)
{
de.properties ["AnonymoususerName"] [0] = vd.anonymoususeusername;
De.properties ["anonymoususeuserpass"] [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.enabledirbrows;
De.properties ["accessssl"] [0] = vd.accesssssl;
De.properties ["AccessScript"] [0] = vd.accessscript;
De.properties ["defaultdoc"] [0] = vd.defaultdoc;
De.properties ["path"] [0] = vd.path;
de.commitchanges ();
}
///
/// Get a 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.anonymoususeusername = (string) de.properties ["anonymoususername"] [0];
vd.anonymoususeuserpass = (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 ["accesssssl"] [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;
}
#ndregion
}
///
/// Virtual directory entity /////
Public Class VirtualDirectory
{
Private bool _read, _execute, _script, _ssl, _write, _authbasic, _authntlm, _indexed, _ndirbrow, _endefaultdoc;
Private string _ausername, _AUSERPASS, _NAME, _PATH;
Private int _flag;
Private string_defaultdoc;
#REGION constructor
///
/// Constructor ///
Public VirtualDirectory ()
{
SetValue ();
}
///
/// Constructor ///
///
Virtual directory name
Public VirtualDirectory (String svirdiRName)
{
SetValue ();
_name = svirdirname;
}
///
/// Constructor ///
///
Virtual directory name
///
Physical path
///
Public VirtualDirectory (String SvirdiRName, String Str Phphypath, String [] Anonymoususer)
{
SetValue ();
_name = svirdirname;
_path = strphypath;
_AUSERNAME = anonymoususer [0];
_AUSERPASS = Anonymoususer [1];
}
#ndregion
///
/// Set the properties of the virtual directory ///
Private void setValue ()
{
_read = true;
_EXECUTE = FALSE;
_script = true;
_ssl = false;
_write = false;
_AUTHBASIC = FALSE;
_authnTlm = true;
_INDEXED = TRUE;
_endirbrow = false;
_ENDEFAULTDOC = True;
_flag = 1;
_DEFAULTDOC = "index.aspx";
_PATH = "C: //";
_AUSERNAME = "IUSR_DEVE-Server";
_AUSERPASS = "IUSR_DEVE-SERVER";
_name = ""
}
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} ///
/// Virtual Directory Collection Class ///
Public Class VirtualDirectories: system.collections.hashtable
{
Public VirtualDirectories ()
{
}
///
/// Search the specified virtual directory ///
///
Virtual directory name
///
Public VirtualDirectory Find (String Strname)
{
Return (VirtualDirectory). [strname];
}
}
}