刀兄写的IIS管理类(C#)
2024-08-29 03:13:55
供稿:网友
///***********************************************************
///************** iis控制管理类 1.0 beta **************
///************** author: 飞刀 **************
///************** http://www.aspcn.com **************
///************** [email protected] **************
///***********************************************************
using system;
using system.data;
using system.directoryservices;
using system.collections;
namespace aspcn.management
{
/// <summary>
/// iismanager 的摘要说明。
/// </summary>
public class iismanager
{
//定义需要使用的
private string _server,_website;
private virtualdirectories _virdirs;
protected system.directoryservices.directoryentry rootfolder;
private bool _batchflag;
public iismanager()
{
//默认情况下使用localhost,即访问本地机
_server = "localhost";
_website = "1";
_batchflag = false;
}
public iismanager(string strserver)
{
_server = strserver;
_website = "1";
_batchflag = false;
}
/// <summary>
/// 定义公共属性
/// </summary>
//server属性定义访问机器的名字,可以是ip与计算名
public string server
{
get{ return _server;}
set{ _server = value;}
}
//website属性定义,为一数字,为方便,使用string
//一般来说第一台主机为1,第二台主机为2,依次类推
public string website
{
get{ return _website; }
set{ _website = value; }
}
//虚拟目录的名字
public virtualdirectories virdirs
{
get{ return _virdirs; }
set{ _virdirs = value;}
}
///<summary>
///定义公共方法
///</summary>
//连接服务器
public void connect()
{
connecttoserver();
}
//为方便重载
public void connect(string strserver)
{
_server = strserver;
connecttoserver();
}
//为方便重载
public void connect(string strserver,string strwebsite)
{
_server = strserver;
_website = strwebsite;
connecttoserver();
}
//判断是否存这个虚拟目录
public bool exists(string strvirdir)
{
return _virdirs.contains(strvirdir);
}
//添加一个虚拟目录
public void create(virtualdirectory newdir)
{
string strpath = "iis://" + _server + "/w3svc/" + _website + "/root/" + newdir.name;
if(!_virdirs.contains(newdir.name) || _batchflag )
{
try
{
//加入到root的children集合中去
directoryentry newvirdir = rootfolder.children.add(newdir.name,"iiswebvirtualdir");
newvirdir.invoke("appcreate",true);
newvirdir.commitchanges();
rootfolder.commitchanges();
//然后更新数据
updatedirinfo(newvirdir,newdir);
}
catch(exception ee)
{
throw new exception(ee.tostring());
}
}
else
{
throw new exception("this virtual directory is already exist.");
}
}
//得到一个虚拟目录
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;
}
//更新一个虚拟目录
public void update(virtualdirectory dir)
{
//判断需要更改的虚拟目录是否存在
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.");
}
}
//删除一个虚拟目录
public void delete(string strvirdir)
{
if(_virdirs.contains(strvirdir))
{
object[] paras = new object[2];
paras[0] = "iiswebvirtualdir"; //表示操作的是虚拟目录
paras[1] = strvirdir;
rootfolder.invoke("delete",paras);
rootfolder.commitchanges();
}
else
{
throw new exception("can''t delete " + strvirdir + ",because it isn''t exists.");
}
}
//批量更新
public void updatebatch()
{
batchupdate(_virdirs);
}
//重载一个:-)
public void updatebatch(virtualdirectories vds)
{
batchupdate(vds);
}
///<summary>
///私有方法
///</summary>
//连接服务器
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);
}
}
//执行批量更新
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;
}
//更新东东
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();
}
//获取虚拟目录集合
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类
/// </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>
/// 构造函数
/// </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>
///定义属性,iisvirtualdir太多属性了
///我只搞了比较重要的一些,其它的大伙需要的自个加吧。
///</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>
/// 集合virtualdirectories
/// </summary>
public class virtualdirectories : system.collections.hashtable
{
public virtualdirectories()
{
}
//添加新的方法
public virtualdirectory find(string strname)
{
return (virtualdirectory)this[strname];
}
}
}
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。