c#操作iis根目录的方法
本文实例讲述了c#操作iis根目录的方法。分享给大家供大家参考。具体实现方法如下:
usingSystem; usingSystem.DirectoryServices; usingSystem.Collections; namespaceIISManagement { ///<summary> ///IISManager的摘要说明。 ///</summary> publicclassIISManager { //定义需要使用的 privatestring_server,_website; privateVirtualDirectories_virdirs; protectedSystem.DirectoryServices.DirectoryEntryrootfolder; privatebool_batchflag; publicIISManager() { //默认情况下使用localhost,即访问本地机 _server="localhost"; _website="1"; _batchflag=false; } publicIISManager(stringstrServer) { _server=strServer; _website="1"; _batchflag=false; } ///<summary> ///定义公共属性 ///</summary> //Server属性定义访问机器的名字,可以是IP与计算名 publicstringServer { get{return_server;} set{_server=value;} } //WebSite属性定义,为一数字,为方便,使用string //一般来说第一台主机为1,第二台主机为2,依次类推 publicstringWebSite { get{return_website;} set{_website=value;} } //虚拟目录的名字 publicVirtualDirectoriesVirDirs { get{return_virdirs;} set{_virdirs=value;} } ///<summary> ///定义公共方法 ///</summary> //连接服务器 publicvoidConnect() { ConnectToServer(); } //为方便重载 publicvoidConnect(stringstrServer) { _server=strServer; ConnectToServer(); } //为方便重载 publicvoidConnect(stringstrServer,stringstrWebSite) { _server=strServer; _website=strWebSite; ConnectToServer(); } //判断是否存这个虚拟目录 publicboolExists(stringstrVirdir) { return_virdirs.Contains(strVirdir); } //添加一个虚拟目录 publicvoidCreate(VirtualDirectorynewdir) { stringstrPath="IIS://"+_server+"/W3SVC/"+_website+"/ROOT/"+ newdir.Name; if(!_virdirs.Contains(newdir.Name)||_batchflag) { try { //加入到ROOT的Children集合中去 DirectoryEntrynewVirDir= rootfolder.Children.Add(newdir.Name,"IIsWebVirtualDir"); newVirDir.Invoke("AppCreate",true); newVirDir.CommitChanges(); rootfolder.CommitChanges(); //然后更新数据 UpdateDirInfo(newVirDir,newdir); } catch(Exceptionee) { thrownewException(ee.ToString()); } } else { thrownewException("Thisvirtualdirectoryisalreadyexist."); } } //得到一个虚拟目录 publicVirtualDirectoryGetVirDir(stringstrVirdir) { VirtualDirectorytmp=null; if(_virdirs.Contains(strVirdir)) { tmp=_virdirs.Find(strVirdir); ((VirtualDirectory)_virdirs[strVirdir]).flag=2; } else { thrownewException("Thisvirtualdirectoryisnotexists"); } returntmp; } //更新一个虚拟目录 publicvoidUpdate(VirtualDirectorydir) { //判断需要更改的虚拟目录是否存在 if(_virdirs.Contains(dir.Name)) { DirectoryEntryode= rootfolder.Children.Find(dir.Name,"IIsWebVirtualDir"); UpdateDirInfo(ode,dir); } else { thrownewException("Thisvirtualdirectoryisnotexists."); } } //删除一个虚拟目录 publicvoidDelete(stringstrVirdir) { if(_virdirs.Contains(strVirdir)) { object[]paras=newobject[2]; paras[0]="IIsWebVirtualDir";//表示操作的是虚拟目录 paras[1]=strVirdir; rootfolder.Invoke("Delete",paras); rootfolder.CommitChanges(); } else { thrownewException("Can''tdelete"+strVirdir+",becauseit isn''texists."); } } //批量更新 publicvoidUpdateBatch() { BatchUpdate(_virdirs); } //重载一个:-) publicvoidUpdateBatch(VirtualDirectoriesvds) { BatchUpdate(vds); } ///<summary> ///私有方法 ///</summary> //连接服务器 privatevoidConnectToServer() { stringstrPath="IIS://"+_server+"/W3SVC/"+_website+"/ROOT"; try { this.rootfolder=newDirectoryEntry(strPath); _virdirs=GetVirDirs(this.rootfolder.Children); } catch(Exceptione) { thrownewException("Can''tconnecttotheserver["+_server +"]...",e); } } //执行批量更新 privatevoidBatchUpdate(VirtualDirectoriesvds) { _batchflag=true; foreach(objectiteminvds.Values) { VirtualDirectoryvd=(VirtualDirectory)item; switch(vd.flag) { case0: break; case1: Create(vd); break; case2: Update(vd); break; } } _batchflag=false; } //更新东东 privatevoidUpdateDirInfo(DirectoryEntryde,VirtualDirectoryvd) { 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(); } //获取虚拟目录集合 privateVirtualDirectoriesGetVirDirs(DirectoryEntriesdes) { VirtualDirectoriestmpdirs=newVirtualDirectories(); foreach(DirectoryEntrydeindes) { if(de.SchemaClassName=="IIsWebVirtualDir") { VirtualDirectoryvd=newVirtualDirectory(); 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); } } returntmpdirs; } } <summary> ///VirtualDirectory类 ///</summary> publicclassVirtualDirectory { privatebool _read,_execute,_script,_ssl,_write,_authbasic,_authntlm,_indexed,_endirbrow,_endefaultdoc; privatestring_ausername,_auserpass,_name,_path; privateint_flag; privatestring_defaultdoc; ///<summary> ///构造函数 ///</summary> publicVirtualDirectory() { SetValue(); } publicVirtualDirectory(stringstrVirDirName) { _name=strVirDirName; SetValue(); } privatevoidSetValue() { _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> publicintflag { get{return_flag;} set{_flag=value;} } publicboolAccessRead { get{return_read;} set{_read=value;} } publicboolAccessWrite { get{return_write;} set{_write=value;} } publicboolAccessExecute { get{return_execute;} set{_execute=value;} } publicboolAccessSSL { get{return_ssl;} set{_ssl=value;} } publicboolAccessScript { get{return_script;} set{_script=value;} } publicboolAuthBasic { get{return_authbasic;} set{_authbasic=value;} } publicboolAuthNTLM { get{return_authntlm;} set{_authntlm=value;} } publicboolContentIndexed { get{return_indexed;} set{_indexed=value;} } publicboolEnableDirBrowsing { get{return_endirbrow;} set{_endirbrow=value;} } publicboolEnableDefaultDoc { get{return_endefaultdoc;} set{_endefaultdoc=value;} } publicstringName { get{return_name;} set{_name=value;} } publicstringPath { get{return_path;} set{_path=value;} } publicstringDefaultDoc { get{return_defaultdoc;} set{_defaultdoc=value;} } publicstringAnonymousUserName { get{return_ausername;} set{_ausername=value;} } publicstringAnonymousUserPass { get{return_auserpass;} set{_auserpass=value;} } } ///<summary> ///集合VirtualDirectories ///</summary> publicclassVirtualDirectories:System.Collections.Hashtable { publicVirtualDirectories() { } //添加新的方法 publicVirtualDirectoryFind(stringstrName) { return(VirtualDirectory)this[strName]; } } }
希望本文所述对大家的C#程序设计有所帮助。