热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

用C#操纵IIS(代码)

用C#操纵IIS(代码)

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 

     /// 

 
     ///  这个类是静态类。用来实现管理IIS的基本操作。 
     ///  管理IIS有两种方式,一是ADSI,一是WMI。由于系统限制的原因,只好选择使用ADSI实现功能。 
     ///  这是一个遗憾。只有等到只有使用IIS 6的时候,才有可能使用WMI来管理系统 
     ///  不过有一个问题就是,我现在也觉得这样的一个方法在本地执行会比较的好。最好不要远程执行。 
     ///  因为那样需要占用相当数量的带宽,即使要远程执行,也是推荐在同一个网段里面执行 
     /// 
 
     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的方法 
         ///  
         ///  根据是否有用户名来判断是否是远程服务器。 
         ///  然后再构造出不同的DirectoryEntry出来 
         /// 
 
         /// DirectoryEntry的路径 
         /// 返回的是DirectoryEntry实例 
         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 添加,删除网站的方法 
         ///  
         ///  创建一个新的网站。根据传过来的信息进行配置 
         /// 
 
         /// 存储的是新网站的信息 
         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(); 
         } 
         ///  
         ///  删除一个网站。根据网站名称删除。 
         /// 
 
         /// 网站名称 
         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 确认网站是否相同 
         ///  
         ///  确定一个新的网站与现有的网站没有相同的。 
         ///  这样防止将非法的数据存放到IIS里面去 
         /// 
 
         /// 网站邦定信息 
         /// 真为可以创建,假为不可以创建 
         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 获取一个网站编号的方法 
         ///  
         ///  获取一个网站的编号。根据网站的ServerBindings或者ServerComment来确定网站编号 
         /// 
 
         ///  
         /// 返回网站的编号 
         /// 表示没有找到网站 
         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的方法 
         ///  
         ///  获取网站系统里面可以使用的最小的ID。 
         ///  这是因为每个网站都需要有一个唯一的编号,而且这个编号越小越好。 
         ///  这里面的算法经过了测试是没有问题的。 
         /// 
 
         /// 最小的id 
         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 


推荐阅读
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • 一、Hadoop来历Hadoop的思想来源于Google在做搜索引擎的时候出现一个很大的问题就是这么多网页我如何才能以最快的速度来搜索到,由于这个问题Google发明 ... [详细]
  • 本文介绍了闭包的定义和运转机制,重点解释了闭包如何能够接触外部函数的作用域中的变量。通过词法作用域的查找规则,闭包可以访问外部函数的作用域。同时还提到了闭包的作用和影响。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • 生成式对抗网络模型综述摘要生成式对抗网络模型(GAN)是基于深度学习的一种强大的生成模型,可以应用于计算机视觉、自然语言处理、半监督学习等重要领域。生成式对抗网络 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 近年来,大数据成为互联网世界的新宠儿,被列入阿里巴巴、谷歌等公司的战略规划中,也在政府报告中频繁提及。据《大数据人才报告》显示,目前全国大数据人才仅46万,未来3-5年将出现高达150万的人才缺口。根据领英报告,数据剖析人才供应指数最低,且跳槽速度最快。中国商业结合会数据剖析专业委员会统计显示,未来中国基础性数据剖析人才缺口将高达1400万。目前BAT企业中,60%以上的招聘职位都是针对大数据人才的。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 本文介绍了C#中生成随机数的三种方法,并分析了其中存在的问题。首先介绍了使用Random类生成随机数的默认方法,但在高并发情况下可能会出现重复的情况。接着通过循环生成了一系列随机数,进一步突显了这个问题。文章指出,随机数生成在任何编程语言中都是必备的功能,但Random类生成的随机数并不可靠。最后,提出了需要寻找其他可靠的随机数生成方法的建议。 ... [详细]
  • qt学习(六)数据库注册用户的实现方法
    本文介绍了在qt学习中实现数据库注册用户的方法,包括登录按钮按下后出现注册页面、账号可用性判断、密码格式判断、邮箱格式判断等步骤。具体实现过程包括UI设计、数据库的创建和各个模块调用数据内容。 ... [详细]
author-avatar
人帅刀快爱美女_915
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有