網上找到自己實驗,并整理了一下:
整個后臺文件如下:
using system;
using system.data;
using system.directoryservices;
using system.collections;
using system.threading;
namespace aspcn.management
{
/// <summary>
/// iismanager 的摘要說明。
/// </summary>
public class iismanager
{
/// <summary>
/// 應用程序的主入口點。
/// </summary>
[stathread]
static void main(string[] args)
{
iismanager test = new iismanager();
test.connect();
virtualdirectory vd = new virtualdirectory();
vd.path = @"c:/inetpub/wwwroot/項目文件夾名稱";
vd.name = "bkdeip/webui/mobile";
//if(test.exists(vd.name))
//{
//test.delete(vd.name);
//}
//else
//{
//test.create(vd);
//}
test.create(vd);
}
//定義需要使用的
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);
}
}
//執(zhí)行批量更新
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];
}
}
}
謝謝閱讀!
新聞熱點
疑難解答
圖片精選