热门标签 | 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 


推荐阅读
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本周信息安全小组主要进行了CTF竞赛相关技能的学习,包括HTML和CSS的基础知识、逆向工程的初步探索以及整数溢出漏洞的学习。此外,还掌握了Linux命令行操作及互联网工作原理的基本概念。 ... [详细]
  • 计算机网络复习:第五章 网络层控制平面
    本文探讨了网络层的控制平面,包括转发和路由选择的基本原理。转发在数据平面上实现,通过配置路由器中的转发表完成;而路由选择则在控制平面上进行,涉及路由器中路由表的配置与更新。此外,文章还介绍了ICMP协议、两种控制平面的实现方法、路由选择算法及其分类等内容。 ... [详细]
  • 本文将介绍如何使用 Go 语言编写和运行一个简单的“Hello, World!”程序。内容涵盖开发环境配置、代码结构解析及执行步骤。 ... [详细]
  • 线性Kalman滤波器在多自由度车辆悬架主动控制中的应用研究
    本文探讨了线性Kalman滤波器(LKF)在不同自由度(2、4、7)的车辆悬架系统中进行主动控制的应用。通过详细的仿真分析,展示了LKF在提升悬架性能方面的潜力,并总结了调参过程中的关键要点。 ... [详细]
  • 本文探讨了Hive中内部表和外部表的区别及其在HDFS上的路径映射,详细解释了两者的创建、加载及删除操作,并提供了查看表详细信息的方法。通过对比这两种表类型,帮助读者理解如何更好地管理和保护数据。 ... [详细]
  • C++实现经典排序算法
    本文详细介绍了七种经典的排序算法及其性能分析。每种算法的平均、最坏和最好情况的时间复杂度、辅助空间需求以及稳定性都被列出,帮助读者全面了解这些排序方法的特点。 ... [详细]
  • 本文介绍如何利用动态规划算法解决经典的0-1背包问题。通过具体实例和代码实现,详细解释了在给定容量的背包中选择若干物品以最大化总价值的过程。 ... [详细]
  • 本文详细探讨了Java中的24种设计模式及其应用,并介绍了七大面向对象设计原则。通过创建型、结构型和行为型模式的分类,帮助开发者更好地理解和应用这些模式,提升代码质量和可维护性。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 题目描述:给定n个半开区间[a, b),要求使用两个互不重叠的记录器,求最多可以记录多少个区间。解决方案采用贪心算法,通过排序和遍历实现最优解。 ... [详细]
  • 深入理解C++中的KMP算法:高效字符串匹配的利器
    本文详细介绍C++中实现KMP算法的方法,探讨其在字符串匹配问题上的优势。通过对比暴力匹配(BF)算法,展示KMP算法如何利用前缀表优化匹配过程,显著提升效率。 ... [详细]
  • 探讨一个显示数字的故障计算器,它支持两种操作:将当前数字乘以2或减去1。本文将详细介绍如何用最少的操作次数将初始值X转换为目标值Y。 ... [详细]
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社区 版权所有