C#操作IIS

代码如下:程序代码
   usingSystem;
   usingSystem.Collections;
   usingSystem.DirectoryServices;
   usingSystem.Text.RegularExpressions;
  
   namespacenabooColl.Web
   {
   ///<summary>
   ///IIS管理类,可以实现创建站点、虚拟目录,删除虚拟目录等
   ///</summary>
   publicclassIISManager
   {
   privatestring_server,_website,_AnonymousUserPass,_AnonymousUserName;
   protectedDirectoryEntryrootfolder;
  
   privatestring_serverip,_domain,_webSiteName,_port,_path;
  
   #region构造函数
   ///<summary>
   ///构造函数
   ///</summary>
   publicIISManager()
   {
   //默认情况下使用localhost,即访问本地机
   _server="localhost";
   _website="1";
   }
  
   ///<summary>
   ///构造函数
   ///</summary>
   ///<paramname="strServer">服务器</param>
   publicIISManager(stringstrServer)
   {
   _server=strServer;
   _website="1";
   }
  
   ///<summary>
   ///构造函数
   ///</summary>
   ///<paramname="strServer">服务器</param>
   ///<paramname="website">站点,每一个站点为1,第二个站点为2,依此类推</param>
   publicIISManager(stringstrServer,intwebsite)
   {
   _server=strServer;
   _website=website.ToString();
   }
   #endregion
  
   #region定义公共属性
  
  
   #region新建一个网站必须设置的五个参数
  
   ///<summary>
   ///用于网站标题,描述
   ///</summary>
   publicstringWebSiteName
   {
   get{return_webSiteName;}
   set{_webSiteName=value;}
   }
  
   ///<summary>
   ///新建立网站的端口,一般设置为80
   ///</summary>
   publicstringPort
   {
   get{return_port;}
   set{_port=value;}
   }
  
   ///<summary>
   ///新建立网站的物理路径
   ///</summary>
   publicstringPath
   {
   get{return_path;}
   set{_path=value;}
   }
  
   ///<summary>
   ///服务器的IP地址
   ///</summary>
   publicstringServerIP
   {
   get{return_serverip;}
   set{_serverip=value;}
  
   }
  
   ///<summary>
   ///网站访问的域名
   ///</summary>
   publicstringDoMain
   {
   get{return_domain;}
   set{_domain=value;}
  
   }
  
   #endregion
  
   ///<summary>
   ///匿名访问用户
   ///</summary>
   publicstringAnonymousUserName
   {
   get{return_AnonymousUserName;}
   set{_AnonymousUserName=value;}
   }
  
   ///<summary>
   ///匿名访问用户密码
   ///</summary>
   publicstringAnonymousUserPass
   {
   get{return_AnonymousUserPass;}
   set{_AnonymousUserPass=value;}
   }
  
   ///<summary>
   ///服务器,可以是IP或计算名
   ///</summary>
   publicstringServer
   {
   get{return_server;}
   set{_server=value;}
   }
  
   ///<summary>
   ///站点,一般来说第一台主机为1,第二台主机为2,依次类推
   ///</summary>
   publicintWebSite
   {
   get{returnConvert.ToInt32(_website);}
   set{_website=Convert.ToString(value);}
   }
  
  
   #endregion
  
  
   ///<summary>
   ///连接服务器
   ///</summary>
   publicvoidConnect()
   {
   ConnectToServer();
   }
  
   ///<summary>
   ///连接服务器
   ///</summary>
   ///<paramname="strServer">服务器名称</param>
   publicvoidConnect(stringstrServer)
   {
   _server=strServer;
   ConnectToServer();
   }
   ///<summary>
   ///连接服务器
   ///</summary>
   ///<paramname="strServer">服务器名称</param>
   ///<paramname="strWebSite">站点,一般来说第一台主机为1,第二台主机为2,依次类推</param>
   publicvoidConnect(stringstrServer,stringstrWebSite)
   {
   _server=strServer;
   _website=strWebSite;
   ConnectToServer();
   }
  
  
   #region获取新网站id的方法
   ///<summary>
   ///获取网站系统里面可以使用的最小的ID。
   ///这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。
   ///这里面的算法经过了测试是没有问题的。
   ///</summary>
   ///<returns>最小的id</returns>
  
   publicstringGetNewWebSiteID()
   {
   ArrayListlist=newArrayList();
   stringtmpStr;
   stringentPath=String.Format("IIS://{0}/w3svc",this._server);
   DirectoryEntryent=newDirectoryEntry(entPath);
   foreach(DirectoryEntrychildinent.Children)
   {
   if(child.SchemaClassName=="IIsWebServer")
   {
   tmpStr=child.Name.ToString();
   list.Add(Convert.ToInt32(tmpStr));
   }
   }
   list.Sort();
   inti=1;
   foreach(intjinlist)
   {
   if(i==j)
   {
   i++;
   }
   }
   returni.ToString();
   }
   #endregion
  
   #region新建立一个IISWEB站点
  
   ///<summary>
   ///添加一个站点
   ///</summary>
   publicstringCreateWebSite()
   {
   intsiteID=int.Parse(GetNewWebSiteID());
   try
   {
   DirectoryEntryroot=newDirectoryEntry("IIS://"+this._server+"/W3SVC");
  
   if(!EnsureNewSiteEnavaible(this._serverip+":"+this._port+":"+this._domain))
   {
   return"此用户名的网站已经存在!";
   }
   else
   {
   DirectoryEntrysite=(DirectoryEntry)root.Invoke("Create","IIsWebServer",siteID);
   site.Invoke("Put","ServerComment",this._webSiteName);
   site.Invoke("Put","KeyType","IIsWebServer");
   site.Invoke("Put","ServerBindings",this._serverip+":"+this._port+":"+this._domain);
   site.Invoke("Put","ServerState",2);
   site.Invoke("Put","FrontPageWeb",1);
   site.Invoke("Put","DefaultDoc","index.aspx,index.html,index.html,default.aspx,default.htm,default.html");
   site.Invoke("Put","ServerAutoStart",1);
   site.Invoke("Put","ServerSize",1);
   site.Invoke("SetInfo");
  
   DirectoryEntrysiteVDir=site.Children.Add("Root","IISWebVirtualDir");
   siteVDir.Properties["AppIsolated"][0]=2;
   siteVDir.Properties["Path"][0]=this._path;
   siteVDir.Properties["AccessFlags"][0]=513;
   siteVDir.Properties["FrontPageWeb"][0]=1;
   siteVDir.Properties["AppRoot"][0]="LM/W3SVC/"+siteID+"/Root";
   siteVDir.Properties["AppFriendlyName"][0]="ROOT";
   siteVDir.CommitChanges();
   site.CommitChanges();
  
   return"创建站点成功!";
   }
  
  
   }
   catch(Exceptionex)
   {
   returnex.Message;
   }
   }
  
   #endregion
  
   #region删除一个网站
  
   ///<summary>
   ///删除一个网站。根据网站名称删除。
   ///</summary>
   ///<paramname="siteName">网站名称</param>
  
   publicvoidDeleteWebSiteByName(stringsiteName)
   {
   stringsiteNum=GetWebSiteNum(siteName);
   stringsiteEntPath=String.Format("IIS://{0}/w3svc/{1}",this._server,siteNum);
   DirectoryEntrysiteEntry=newDirectoryEntry(siteEntPath);
   stringrootPath=String.Format("IIS://{0}/w3svc",this._server);
   DirectoryEntryrootEntry=newDirectoryEntry(rootPath);
   rootEntry.Children.Remove(siteEntry);
   rootEntry.CommitChanges();
   }
  
   #endregion
  
  
   #region获取一个网站编号的方法
   ///<summary>
   ///获取一个网站的编号。根据网站的ServerBindings或者ServerComment来确定网站编号
   ///</summary>
   ///<paramname="siteName"></param>
   ///<returns>返回网站的编号</returns>
  
   publicstringGetWebSiteNum(stringsiteName)
   {
   Regexregex=newRegex(siteName);
   stringtmpStr;
   stringentPath=String.Format("IIS://{0}/w3svc",this._server);
   DirectoryEntryent=newDirectoryEntry(entPath);
   foreach(DirectoryEntrychildinent.Children)
   {
   if(child.SchemaClassName=="IIsWebServer")
   {
   if(child.Properties["ServerBindings"].Value!=null)
   {
   tmpStr=child.Properties["ServerBindings"].Value.ToString();
   if(regex.Match(tmpStr).Success)
   {
   returnchild.Name;
   }
   }
   if(child.Properties["ServerComment"].Value!=null)
   {
   tmpStr=child.Properties["ServerComment"].Value.ToString();
   if(regex.Match(tmpStr).Success)
   {
   returnchild.Name;
   }
   }
   }
   }
   return"没有找到要删除的站点";
   }
  
   #endregion
  
  
   #regionStart和Stop网站的方法
  
   publicvoidStartWebSite(stringsiteName)
   {
   stringsiteNum=GetWebSiteNum(siteName);
   stringsiteEntPath=String.Format("IIS://{0}/w3svc/{1}",this._server,siteNum);
   DirectoryEntrysiteEntry=newDirectoryEntry(siteEntPath);
   siteEntry.Invoke("Start",newobject[]{});
   }
  
   publicvoidStopWebSite(stringsiteName)
   {
   stringsiteNum=GetWebSiteNum(siteName);
   stringsiteEntPath=String.Format("IIS://{0}/w3svc/{1}",this._server,siteNum);
   DirectoryEntrysiteEntry=newDirectoryEntry(siteEntPath);
   siteEntry.Invoke("Stop",newobject[]{});
   }
  
   #endregion
  
  
   #region确认网站是否相同
  
   publicboolEnsureNewSiteEnavaible(stringbindStr)
   {
   stringentPath=String.Format("IIS://{0}/w3svc",this._server);
   DirectoryEntryent=newDirectoryEntry(entPath);
   foreach(DirectoryEntrychildinent.Children)
   {
   if(child.SchemaClassName=="IIsWebServer")
   {
   if(child.Properties["ServerBindings"].Value!=null)
   {
   if(child.Properties["ServerBindings"].Value.ToString()==bindStr)
   {
   returnfalse;
   }
   }
   }
   }
   returntrue;
   }
  
   #endregion
  
  
   #region私有方法
  
   ///<summary>
   ///关闭当前对象
   ///</summary>
   publicvoidClose()
   {
   rootfolder.Dispose();
   }
  
   ///<summary>
   ///连接服务器
   ///</summary>
   privatevoidConnectToServer()
   {
   stringstrPath="IIS://"+_server+"/W3SVC/"+_website+"/ROOT";
   try
   {
   this.rootfolder=newDirectoryEntry(strPath);
   }
   catch(Exception)
   {
   Response.Write("无法连接到服务器:"+_server);
   }
   }
   }
   #endregion
   }
  
  
  
  记得引用System.DirectoryServices.dll
  
  
  
  上面这个文件,放在类文件里去。。
  
  下面讲的话,关于他的使用方法。
  
  
  程序代码
  
  
  这是新建站点
  
   privatevoidcreatIisWebSite()
   {
   IISManageriis=newIISManager();
  
   iis.Connect();
   iis.ServerIP="192.168.0.177";
   iis.DoMain="demo.caspnet.com.cn";
   iis.WebSiteName="demo.caspnet.com.cn";
   iis.Port="80";
   iis.Path=@"e:/zhming/website/";
  
   stringresult=iis.CreateWebSite();
  
   Response.Write(result);
   iis.Close();
   }
  
  
  程序代码
  
  
  
  删除站点,站点名称可以通过textbox输入
  
   stringsitename=this.TextBox1.Text;
   IISManageriis=newIISManager();
   iis.Connect();
   iis.DeleteWebSiteByName(sitename);
   iis.Close();
  
  
  
  程序代码
  
  
  启动站点,站点名称可以通过textbox输入
  
  
   stringsitename=this.TextBox1.Text;
   IISManageriis=newIISManager();
   iis.Connect();
   iis.StartWebSite(sitename);
   iis.Close();
  
  
  程序代码
  
  
  停止站点,站点名称可以通过textbox输入
  
  
   stringsitename=this.TextBox1.Text;
   IISManageriis=newIISManager();
   iis.Connect();
   iis.StopWebSite(sitename);
   iis.Close();
  
  
  最后一步
  在web.config里输入,确保你有足够的权限去操作IIS站点。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值