(1) Createwebdir.cs use example
String sserver = "localhost";
String VirtualDir = "BUG2004"; // Virtual Directory
String pathdir = @ "d: / mywebdirtest / bug2000" // physical directory
IISMANAGER IISMG = New IISMANAGER (SSERVER);
Iismg.connect ();
ISMG.EXISTS (Virtualdir]))
{
Console.write (VirtualDir "IS EXIST!");
}
Else
{
Myini.iniWriteValue ("WebDir", "WebDirexist", "False");
Iismg.get_anonymoususeuser ();
String [] anonymoususer = new string [2];
Anonymoususer [0] = IISMG.AnonymoususerName;
Anonymoususer [1] = IISMG.AnonymousUserpass;
VirtualDirectory newvirdir = New VirtualDirectory (VirtualDir, Pathdir, Anonymoususe);
IISMG.CREATE (NewVirdir))
Console.write (VirtualDir "creates success!");
Else
Console.Write (VirtualDir "creates unsuccessful!");
}
Iismg.close ();
(2) CREATEBDIR.CS is reposted, I have made some perfect and modified
/// ********************************************************* ************
/// ************* IIS control management class **************
/// ************* Repost self: flying knife http://www.aspcn.com *************
/// ********************************************************* ************
Using system;
Using system.data;
Using System.directoryServices;
Using system.collections;
Namespace CreateWebdir
{
///
/// IISMANAGER's summary description.
/// summary>
Public Class Iismanager
{
// Definition needs to be used
Private string _server, _website, _anonymoususeuserpass, _anonymoususername;
Private VirtualDirectories_virdirs;
Protected system.directoryServices.directoryEntry Rootfolder;
Private bool _batchflag;
Public IISMANAGER ()
{
// By default, LocalHost is used, that is, access the local machine
_server = "localhost";
_Website = "1"; _ BATCHFLAG = FALSE;
}
Public IISMANAGER (STRING STRSERVER)
{
_server = strserver;
_Website = "1";
_BATCHFLAG = FALSE;
}
///
// Define public properties
/// summary>
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";
}
}
Public String AnonymousUsername
{
Get {return_Anonymoususername;
Set {_anonymoususername = value;
}
Public String AnonymousUserpass
{
Get {return_Anonymoususerpass;}
Set {_anonymoususeuserpass = value;
}
// Server property defines the name of the access machine, which can be IP and calculation names
Public String Server
{
Get {return _server;}
Set {_server = value;}
}
// Website property definition, for a number, for convenience, use String
// Generally, the first host is 1, the second host is 2, and the secondary
Public String Website
{
Get {return _Website;
Set {_website = value;}
}
// Name of the virtual directory
Public VirtualDirectories Virdirs
{
Get {return _virdirs;
Set {_virdirs = value;}
}
///
// / Define public methods
/// summary>
//connect to the server
Public void connect ()
{
ConnectTOSERVER ();
}
/ / For convenient overload
Public Void Connect (String STRSERVER)
{
_server = strserver;
ConnectToserver ();
/ / For convenient overload
Public Void Connect (String Strserver, String StrWebsite)
{
_server = strserver;
_Website = StrWebsite;
ConnectTOSERVER ();
}
/ / Judgment Whether to save this virtual directory
Public Bool EXISTS (String Strvirdir)
{
Return_Virdirs.contains (STRVIRDIR);
}
// Add a virtual directory
Public Bool Create (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 EE)
{
// throw new exception (Ee.toString ());
Return False;
}
}
Else
{
Return True;
// 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
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 the virtual directory
Paras [1] = STRVIRDIR;
Rootfolder.Invoke ("delete", parac;
Rootfolder.commitchanges ();
}
Else
{
// Throw new Exception ("Can''t delete" StrvirDir ", Because IT ISN''t exists");
}
}
// Batch update
Public void updatebatch ()
{
BatchUpdate (_VIRDIRS);
}
// Reserve one :-)
Public void Updatebatch (VirtualDirectories VDS)
{
BatchUpdate (VDS);
}
///
/// Private method
/// summary>
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 E)
{
// throw new Exception ("Can''t Connect to the Server [" _Server "] ...", E);
}
}
/ / Execute 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.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 ["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;
}
}
///
/// VirtualDirectory class
/// summary>
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;
///
/// Constructor
/// summary>
Public VirtualDirectory ()
{
SetValue ();
}
Public VirtualDirectory (String svirdiRName)
{
SetValue ();
_name = svirdirname;
}
// svirdirName: virtual path;
// StrPhypath: Physics Path
Public VirtualDirectory (String SvirdiRName, String Str Phphypath, String [] Anonymoususer)
{
SetValue ();
_name = svirdirname;
_path = strphypath;
_AUSERNAME = anonymoususer [0];
_AUSERPASS = Anonymoususer [1];
}
Private void setValue ()
{
_read = true; _execute = false; _ssl = false; _write = false; _write = false;
_authntlm = true; _indexed = true; _ndefaultdoc = true;
_flag = 1;
_defaultdoc = "default.htm, default.aspx, default.asp, index.htm";
_PATH = "C: //";
_AUSERNAME = "IUSR_DEVE-Server"; _ auserpass = "iUSR_DEVE-Server"; _ name = ""
}
///
// Define attributes, iisvirtualdir too much attribute
/// I only have some important, and other big guys need 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 {_ndirbrow = 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;
}
}
///
// Collection VirtualDirectories
/// summary>
Public Class VirtualDirectories: system.collections.hashtable
{
Public VirtualDirectories ()
{
}
// Add new method
Public VirtualDirectory Find (String Strname)
{
Return (VirtualDirectory). [strname];
}
}
}