当前位置: 移动技术网 > IT编程>开发语言>c# > 用C#操纵IIS(代码)

用C#操纵IIS(代码)

2019年07月18日  | 移动技术网IT编程  | 我要评论

using system; 
using system.directoryservices; 
using system.collections; 
using system.text.regularexpressions; 
using system.text; 
/** 
 * @author 吴海燕 
 * @email  wuhy80-usual@yahoo.com 
 * 2004-6-25 第一版 
 */  
namespace wuhy.toolbox 

     /// <summary> 
     ///  这个类是静态类。用来实现管理iis的基本操作。 
     ///  管理iis有两种方式,一是adsi,一是wmi。由于系统限制的原因,只好选择使用adsi实现功能。 
     ///  这是一个遗憾。只有等到只有使用iis 6的时候,才有可能使用wmi来管理系统 
     ///  不过有一个问题就是,我现在也觉得这样的一个方法在本地执行会比较的好。最好不要远程执行。 
     ///  因为那样需要占用相当数量的带宽,即使要远程执行,也是推荐在同一个网段里面执行 
     /// </summary> 
     public class iisadminlib 
     { 
          #region username,password,hostname的定义 
         public static string hostname 
         { 
              get 
              { 
                   return hostname; 
              } 
              set 
              { 
                   hostname = value; 
              } 
         } 
         public static string username 
         { 
              get 
              { 
                   return username; 
              } 
              set 
              { 
                   username = value; 
              } 
         } 
         public static string password 
         { 
              get 
              { 
                   return password; 
              } 
              set 
              { 
                   if(username.length <= 1) 
                   { 
                       throw new argumentexception("还没有指定好用户名。请先指定用户名"); 
                   } 
                   password = value; 
              } 
         } 
         public static void remoteconfig(string hostname, string username, string password) 
         { 
              hostname = hostname; 
              username = username; 
              password = password; 
         } 
          private static string hostname = "localhost"; 
          private static string username; 
          private static string password; 
          #endregion 
          #region 根据路径构造entry的方法 
         /// <summary> 
         ///  根据是否有用户名来判断是否是远程服务器。 
         ///  然后再构造出不同的directoryentry出来 
         /// </summary> 
         /// <param name="entpath">directoryentry的路径</param> 
         /// <returns>返回的是directoryentry实例</returns> 
         public static directoryentry getdirectoryentry(string entpath) 
         { 
              directoryentry ent; 
              if(username == null) 
              { 
                   ent = new directoryentry(entpath); 
              } 
              else 
              { 
                   //    ent = new directoryentry(entpath, hostname+"\\"+username, password, authenticationtypes.secure); 
                   ent = new directoryentry(entpath, username, password, authenticationtypes.secure); 
              } 
              return ent; 
         } 
          #endregion 
          #region 添加,删除网站的方法 
         /// <summary> 
         ///  创建一个新的网站。根据传过来的信息进行配置 
         /// </summary> 
         /// <param name="siteinfo">存储的是新网站的信息</param> 
         public static void createnewwebsite(newwebsiteinfo siteinfo) 
         { 
              if(! ensurenewsiteenavaible(siteinfo.bindstring)) 
              { 
                   throw new duplicatedwebsiteexception("已经有了这样的网站了。" + environment.newline + siteinfo.bindstring); 
              } 
              string entpath = string.format("iis://{0}/w3svc", hostname); 
              directoryentry rootentry = getdirectoryentry(entpath); 
              string newsitenum = getnewwebsiteid(); 
              directoryentry newsiteentry = rootentry.children.add(newsitenum, "iiswebserver"); 
              newsiteentry.commitchanges(); 
              newsiteentry.properties["serverbindings"].value = siteinfo.bindstring; 
              newsiteentry.properties["servercomment"].value = siteinfo.commentofwebsite; 
              newsiteentry.commitchanges(); 
              directoryentry vdentry = newsiteentry.children.add("root", "iiswebvirtualdir"); 
              vdentry.commitchanges(); 
              vdentry.properties["path"].value = siteinfo.webpath; 
              vdentry.commitchanges(); 
         } 
         /// <summary> 
         ///  删除一个网站。根据网站名称删除。 
         /// </summary> 
         /// <param name="sitename">网站名称</param> 
         public static void deletewebsitebyname(string sitename) 
         { 
              string sitenum = getwebsitenum(sitename); 
              string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum); 
              directoryentry siteentry = getdirectoryentry(siteentpath); 
              string rootpath = string.format("iis://{0}/w3svc", hostname); 
              directoryentry rootentry = getdirectoryentry(rootpath); 
              rootentry.children.remove(siteentry); 
              rootentry.commitchanges(); 
         } 
          #endregion 
          #region start和stop网站的方法 
         public static void startwebsite(string sitename) 
         { 
              string sitenum = getwebsitenum(sitename); 
              string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum); 
              directoryentry siteentry = getdirectoryentry(siteentpath); 
              siteentry.invoke("start", new object[] {}); 
         } 
         public static void stopwebsite(string sitename) 
         { 
              string sitenum = getwebsitenum(sitename); 
              string siteentpath = string.format("iis://{0}/w3svc/{1}", hostname, sitenum); 
              directoryentry siteentry = getdirectoryentry(siteentpath); 
              siteentry.invoke("stop", new object[] {}); 
         } 
          #endregion 
          #region 确认网站是否相同 
         /// <summary> 
         ///  确定一个新的网站与现有的网站没有相同的。 
         ///  这样防止将非法的数据存放到iis里面去 
         /// </summary> 
         /// <param name="bindstr">网站邦定信息</param> 
         /// <returns>真为可以创建,假为不可以创建</returns> 
         public static bool ensurenewsiteenavaible(string bindstr) 
         { 
              string entpath = string.format("iis://{0}/w3svc", hostname); 
              directoryentry ent = getdirectoryentry(entpath); 
              foreach(directoryentry child in ent.children) 
              { 
                   if(child.schemaclassname == "iiswebserver") 
                   { 
                        if(child.properties["serverbindings"].value != null) 
                       { 
                            if(child.properties["serverbindings"].value.tostring() == bindstr) 
                            { 
                                 return false; 
                            } 
                       } 
                   } 
              } 
              return true; 
         } 
          #endregion 
          #region 获取一个网站编号的方法 
         /// <summary> 
         ///  获取一个网站的编号。根据网站的serverbindings或者servercomment来确定网站编号 
         /// </summary> 
         /// <param name="sitename"></param> 
         /// <returns>返回网站的编号</returns> 
         /// <exception cref="notfoundwebsiteexception">表示没有找到网站</exception> 
         public static string getwebsitenum(string sitename) 
         { 
              regex regex = new regex(sitename); 
              string tmpstr; 
              string entpath = string.format("iis://{0}/w3svc", hostname); 
              directoryentry ent = getdirectoryentry(entpath); 
              foreach(directoryentry child in ent.children) 
              { 
                   if(child.schemaclassname == "iiswebserver") 
                   { 
                        if(child.properties["serverbindings"].value != null) 
                       { 
                            tmpstr = child.properties["serverbindings"].value.tostring(); 
                            if(regex.match(tmpstr).success) 
                            { 
                                 return child.name; 
                            } 
                       } 
                        if(child.properties["servercomment"].value != null) 
                       { 
                            tmpstr = child.properties["servercomment"].value.tostring(); 
                            if(regex.match(tmpstr).success) 
                            { 
                                 return child.name; 
                            } 
                       } 
                   } 
              } 
              throw new notfoundwebsiteexception("没有找到我们想要的站点" + sitename); 
         } 
          #endregion 
          #region 获取新网站id的方法 
         /// <summary> 
         ///  获取网站系统里面可以使用的最小的id。 
         ///  这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。 
         ///  这里面的算法经过了测试是没有问题的。 
         /// </summary> 
         /// <returns>最小的id</returns> 
         public static string getnewwebsiteid() 
         { 
              arraylist list = new arraylist(); 
              string tmpstr; 
              string entpath = string.format("iis://{0}/w3svc", hostname); 
              directoryentry ent = getdirectoryentry(entpath); 
              foreach(directoryentry child in ent.children) 
              { 
                   if(child.schemaclassname == "iiswebserver") 
                   { 
                       tmpstr = child.name.tostring(); 
                        list.add(convert.toint32(tmpstr)); 
                   } 
              } 
              list.sort(); 
              int i = 1; 
              foreach(int j in list) 
              { 
                   if(i == j) 
                   { 
                       i++; 
                   } 
              } 
              return i.tostring(); 
         } 
          #endregion 
     } 
     #region 新网站信息结构体 
     public struct newwebsiteinfo 
     { 
          private string hostip;   // the hosts ip address 
          private string portnum;   // the new web sites port.generally is "80" 
          private string descofwebsite; // 网站表示。一般为网站的网站名。例如"www.dns.com.cn" 
          private string commentofwebsite;// 网站注释。一般也为网站的网站名。 
          private string webpath;   // 网站的主目录。例如"e:\tmp" 
         public newwebsiteinfo(string hostip, string portnum, string descofwebsite, string commentofwebsite, string webpath) 
         { 
              this.hostip = hostip; 
              this.portnum = portnum; 
              this.descofwebsite = descofwebsite; 
              this.commentofwebsite = commentofwebsite; 
              this.webpath = webpath; 
         } 
         public string bindstring 
         { 
              get 
              { 
                   return string.format("{0}:{1}:{2}", hostip, portnum, descofwebsite); 
              } 
         } 
         public string commentofwebsite 
         { 
              get 
              { 
                   return commentofwebsite; 
              } 
         } 
         public string webpath 
         { 
              get 
              { 
                   return webpath; 
              } 
         } 
     } 
     #endregion 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网