IIS management class (C #) written by the knife

xiaoxiao2021-03-06  120

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

/// ************** 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

{

///

/// IISMANAGER's summary description.

///

Public Class Iismanager

{

// Definition needs to be used

Private string _server, _Website;

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

///

// 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 method

///

//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 Void 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);

}

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

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"; // means 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");

}

}

// Batch update

Public void updatebatch ()

{

BatchUpdate (_VIRDIRS);

}

// Reserve one :-)

Public void Updatebatch (VirtualDirectories VDS)

{

BatchUpdate (VDS);

}

///

/// Private method

///

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

///

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

///

Public VirtualDirectory ()

{

SetValue ();

}

Public VirtualDirectory (String StrvirdiRName)

{

_Name = STRVIRDIRNAME;

SetValue ();

}

Private void setValue ()

{

_read = true; _execute = false; _ssl = false; _Write = false; _write = false; _authntlm = false;

_indexed = false; _ndefaultdoc = false;

_flag = 1;

_defaultdoc = "default.htm, default.aspx, default.asp, index.htm";

_PATH = "C: //";

_AUSERNAME = ""; _aurserpass = "; _ name =" ";

}

///

// Define attributes, iisvirtualdir too much attribute

/// I only have some important, and other big guys need to add it.

///

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

///

Public Class VirtualDirectories: system.collections.hashtable

{

Public VirtualDirectories ()

{

}

// Add new method

Public VirtualDirectory Find (String Strname)

{

Return (VirtualDirectory). [strname];

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-98219.html

New Post(0)