用C#操纵IIS(代码)

 

using System;<o:p></o:p>

using System.DirectoryServices;<o:p></o:p>

using System.Collections;<o:p></o:p>

using System.Text.RegularExpressions;<o:p></o:p>

using System.Text;<o:p></o:p>

 <o:p></o:p>

/**<o:p></o:p>

 * @author 吴海燕<o:p></o:p>

 * @email  wuhy80-usual@yahoo.com<o:p></o:p>

 * 2004-6-25 第一版<o:p></o:p>

 */ <o:p></o:p>

namespace Wuhy.ToolBox<o:p></o:p>

{<o:p></o:p>

     /// <summary><o:p></o:p>

     ///  这个类是静态类。用来实现管理IIS的基本操作。<o:p></o:p>

     ///  管理IIS有两种方式,一是ADSI,一是WMI。由于系统限制的原因,只好选择使用ADSI实现功能。<o:p></o:p>

     ///  这是一个遗憾。只有等到只有使用IIS 6的时候,才有可能使用WMI来管理系统<o:p></o:p>

     ///  不过有一个问题就是,我现在也觉得这样的一个方法在本地执行会比较的好。最好不要远程执行。<o:p></o:p>

     ///  因为那样需要占用相当数量的带宽,即使要远程执行,也是推荐在同一个网段里面执行<o:p></o:p>

     /// </summary><o:p></o:p>

     public class IISAdminLib<o:p></o:p>

     {<o:p></o:p>

          #region UserName,Password,HostName的定义<o:p></o:p>

         public static string HostName<o:p></o:p>

         {<o:p></o:p>

              get<o:p></o:p>

              {<o:p></o:p>

                   return hostName;<o:p></o:p>

              }<o:p></o:p>

              set<o:p></o:p>

              {<o:p></o:p>

                   hostName = value;<o:p></o:p>

              }<o:p></o:p>

         }<o:p></o:p>

 <o:p></o:p>

         public static string UserName<o:p></o:p>

         {<o:p></o:p>

              get<o:p></o:p>

              {<o:p></o:p>

                   return userName;<o:p></o:p>

              }<o:p></o:p>

              set<o:p></o:p>

              {<o:p></o:p>

                   userName = value;<o:p></o:p>

              }<o:p></o:p>

         }<o:p></o:p>

 <o:p></o:p>

         public static string Password<o:p></o:p>

         {<o:p></o:p>

              get<o:p></o:p>

              {<o:p></o:p>

                   return password;<o:p></o:p>

              }<o:p></o:p>

              set<o:p></o:p>

              {<o:p></o:p>

                   if(UserName.Length <= 1)<o:p></o:p>

                   {<o:p></o:p>

                       throw new ArgumentException("还没有指定好用户名。请先指定用户名");<o:p></o:p>

                   }<o:p></o:p>

 <o:p></o:p>

                   password = value;<o:p></o:p>

              }<o:p></o:p>

         }<o:p></o:p>

 <o:p></o:p>

         public static void RemoteConfig(string hostName, string userName, string password)<o:p></o:p>

         {<o:p></o:p>

              HostName = hostName;<o:p></o:p>

              UserName = userName;<o:p></o:p>

              Password = password;<o:p></o:p>

         }<o:p></o:p>

 <o:p></o:p>

          private static string hostName = "localhost";<o:p></o:p>

          private static string userName;<o:p></o:p>

          private static string password;<o:p></o:p>

          #endregion<o:p></o:p>

 <o:p></o:p>

          #region 根据路径构造Entry的方法<o:p></o:p>

         /// <summary><o:p></o:p>

         ///  根据是否有用户名来判断是否是远程服务器。<o:p></o:p>

         ///  然后再构造出不同的DirectoryEntry出来<o:p></o:p>

         /// </summary><o:p></o:p>

         /// <param name="entPath">DirectoryEntry的路径</param><o:p></o:p>

         /// <returns>返回的是DirectoryEntry实例</returns><o:p></o:p>

         public static DirectoryEntry GetDirectoryEntry(string entPath)<o:p></o:p>

         {<o:p></o:p>

              DirectoryEntry ent;<o:p></o:p>

 <o:p></o:p>

              if(UserName == null)<o:p></o:p>

              {<o:p></o:p>

                   ent = new DirectoryEntry(entPath);<o:p></o:p>

              }<o:p></o:p>

              else<o:p></o:p>

              {<o:p></o:p>

                   //    ent = new DirectoryEntry(entPath, HostName+"\\"+UserName, Password, AuthenticationTypes.Secure);<o:p></o:p>

                   ent = new DirectoryEntry(entPath, UserName, Password, AuthenticationTypes.Secure);<o:p></o:p>

              }<o:p></o:p>

 <o:p></o:p>

              return ent;<o:p></o:p>

         }<o:p></o:p>

          #endregion<o:p></o:p>

 <o:p></o:p>

          #region 添加,删除网站的方法<o:p></o:p>

         /// <summary><o:p></o:p>

         ///  创建一个新的网站。根据传过来的信息进行配置<o:p></o:p>

         /// </summary><o:p></o:p>

         /// <param name="siteInfo">存储的是新网站的信息</param><o:p></o:p>

         public static void CreateNewWebSite(NewWebSiteInfo siteInfo)<o:p></o:p>

         {<o:p></o:p>

              if(! EnsureNewSiteEnavaible(siteInfo.BindString))<o:p></o:p>

              {<o:p></o:p>

                   throw new DuplicatedWebSiteException("已经有了这样的网站了。" + Environment.NewLine + siteInfo.BindString);<o:p></o:p>

              }<o:p></o:p>

 <o:p></o:p>

              string entPath = String.Format("IIS://{0}/w3svc", HostName);<o:p></o:p>

              DirectoryEntry rootEntry = GetDirectoryEntry(entPath);<o:p></o:p>

 <o:p></o:p>

              string newSiteNum = GetNewWebSiteID();<o:p></o:p>

              DirectoryEntry newSiteEntry = rootEntry.Children.Add(newSiteNum, "IIsWebServer");<o:p></o:p>

              newSiteEntry.CommitChanges();<o:p></o:p>

 <o:p></o:p>

              newSiteEntry.Properties["ServerBindings"].Value = siteInfo.BindString;<o:p></o:p>

              newSiteEntry.Properties["ServerComment"].Value = siteInfo.CommentOfWebSite;<o:p></o:p>

              newSiteEntry.CommitChanges();<o:p></o:p>

 <o:p></o:p>

              DirectoryEntry vdEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");<o:p></o:p>

              vdEntry.CommitChanges();<o:p></o:p>

 <o:p></o:p>

              vdEntry.Properties["Path"].Value = siteInfo.WebPath;<o:p></o:p>

              vdEntry.CommitChanges();<o:p></o:p>

         }<o:p></o:p>

 <o:p></o:p>

         /// <summary><o:p></o:p>

         ///  删除一个网站。根据网站名称删除。<o:p></o:p>

         /// </summary><o:p></o:p>

         /// <param name="siteName">网站名称</param><o:p></o:p>

         public static void DeleteWebSiteByName(string siteName)<o:p></o:p>

         {<o:p></o:p>

              string siteNum = GetWebSiteNum(siteName);<o:p></o:p>

              string siteEntPath = String.Format("IIS://{0}/w3svc/{1}", HostName, siteNum);<o:p></o:p>

              DirectoryEntry siteEntry = GetDirectoryEntry(siteEntPath);<o:p></o:p>

 <o:p></o:p>

              string rootPath = String.Format("IIS://{0}/w3svc", HostName);<o:p></o:p>

              DirectoryEntry rootEntry = GetDirectoryEntry(rootPath);<o:p></o:p>

 <o:p></o:p>

              rootEntry.Children.Remove(siteEntry);<o:p></o:p>

              rootEntry.CommitChanges();<o:p></o:p>

         }<o:p></o:p>

          #endregion<o:p></o:p>

 <o:p></o:p>

          #region Start和Stop网站的方法<o:p></o:p>

         public static void StartWebSite(string siteName)<o:p></o:p>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值