当前位置: 首页 > 编程日记 > 正文

用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

{

     /// <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

}

相关文章:

linux 内核参数调整说明

linux 内核参数调整说明 所有的TCP/IP调优参数都位于/proc/sys/net/目录。例如, 下面是最重要的一些调优参数, 后面是它们的含义: 1. /proc/sys/net/core/rmem_max — 最大的TCP数据接收缓冲。2. /proc/sys/net/core/wmem_max — 最大的TCP数据发送缓冲。3. /proc/sys/net/ipv4…

Java 最高均薪 19015 元! 9 月程序员工资出炉,你拖后腿了吗?

在全员争当码农的时代&#xff0c;如果你也想学一门编程语言&#xff0c;那么&#xff0c;我要告诉你&#xff0c;Java 是编程语言中不可撼动的王者。有点难理解&#xff1f;先看个排行榜???? 来自权威开发语言排行榜 TIOBE 的数据&#xff08;截止到 2020 年 4 月&#x…

java 基础知识八 正则表达式

正则表达式 是一种可以用于模式匹配和替换的规范&#xff0c; 一个正则表达式就是由普通的字符&#xff08;例如字符a到z&#xff09;以及特殊字符&#xff08;元字符&#xff09;组成的文字模式&#xff0c; 用以描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为…

PHP中Session的使用

启用配置//修改php.ini中的session.auto_start 0 为 session.auto_start 1session_start();$_SESSION[username]"HM";

《Two Dozen Short Lessons in Haskell》学习(八)- Function Types, Classes, and Polymorphism

《Two Dozen Short Lessons in Haskell》&#xff08;Copyright © 1995, 1996, 1997 by Rex Page&#xff0c;有人翻译为Haskell二十四学时教程&#xff0c;该书如果不用于赢利&#xff0c;可以任意发布&#xff0c;但需要保留他们的copyright&#xff09;这本书是学习 Ha…

图神经网络快速爆发,最新进展都在这里了

译者 | 刘畅出品 | AI科技大本营&#xff08;rgznai100&#xff09;近年来&#xff0c;图神经网络&#xff08;GNNs&#xff09;发展迅速&#xff0c;最近的会议上发表了大量相关的研究论文。本文作者正在整理一个GNN的简短介绍和最新研究报告的摘要。希望这对任何准备进入该领…

css去掉a标签点击后的虚线框

outline是css3的一个属性&#xff0c;用的很少。 声明&#xff0c;这是个不能兼容的css属性&#xff0c;在ie6、ie7、遨游浏览器都不兼容。 outline控制的到底是什么呢&#xff1f; 当聚焦a标签的时候&#xff0c;在a标签的区域周围会有一个虚线的框&#xff0c;这个框不同于bo…

在SQL Server中保存和输出任意类型的文件

我们可以把任意类型的文件保存到SQL Server中&#xff0c;在进行例子之前&#xff0c;先建立测试用表格&#xff0c;TestFile.sql&#xff1a;if exists (select * from dbo.sysobjects where id object_id(N[dbo].[TestFiles]) and OBJECTPROPERTY(id, NIsUserTable) 1) dro…

工作中InnoDB引擎数据库主从复制同步心得

近期将公司的MySQL架构升级了&#xff0c;由原先的一主多从换成了DRBDHeartbeat双主多从&#xff0c;正好手上有一个电子商务网站新项目也要上线了&#xff0c;用的是DRBDHeartbeat双主一从&#xff0c;由于此过程还是有别于以前的MyISAM引擎的&#xff0c;所以这里也将其心得归…

面试官:因为这个语言,我淘汰了90%的人!

很多人都有这样的经历&#xff1a;大量重复性工作&#xff1b;日报、周报、各种报&#xff0c;无穷无尽&#xff1b;不计其数的数据提取琐碎繁杂的事务让工作的效率极低。如果可以一键完成就好了。对这些问题来说&#xff0c;最高效的解决途径就是 Python。1991 年&#xff0c;…

SQL Server不能启动

SQL Server不能正常启动 I had a similar issue after uninstalling Visual Studio 2010 (which autmatically came with a Visual Studio Express 2013 install). I solved it by going through the follwing steps. Installing Visual Studio 2010 shell from here: https://…

ASP.NET 配置节架构

ASP.NET 配置节架构包含控制 ASP.NET Web 应用程序行为的元素。如果为属性指定了默认值&#xff0c;则该默认值是在 Machine.config 文件中设置的&#xff0c;该文件的路径是 systemroot/Microsoft.NET/Framework/versionNumber/CONFIG/Machine.config。 <configuration>…

IEEE迎来首位华人主席,马里兰大学终身教授刘国瑞当选

10月12日&#xff0c;IEEE宣布马里兰大学终身教授刘国瑞&#xff08;K. J. Ray Liu&#xff09;当选为2021年IEEE主席&#xff0c;他也是首位当选IEEE主席的华人学者&#xff0c;他将在明年1月开始接任现任IEEE主席Susan K. Kathy Land的职务。 在此次IEEE候选主席竞选中&#…

Visual C++ 2010 简介

VC是用来创建基于 Microsoft Windows 和 Microsoft .NET 的应用程序 原文地址&#xff1a;http://msdn.microsoft.com/zh-cn/library/60k1461a%28vvs.100%29.aspx提供了强大而灵活的开发环境&#xff0c;用于创建基于 Microsoft Windows 和 Microsoft .NET 的应用程序。您可以在…

Linux网络编程:基于UDP的程序开发回顾篇

基于无连接的UDP程序设计 同样&#xff0c;在开发基于UDP的应用程序时&#xff0c;其主要流程如下&#xff1a; 对于面向无连接的UDP应用程序在开发过程中服务端和客户端的操作流程基本差不多。对比面向连接的TCP程序&#xff0c;服务端少了listen和accept函数。前面我们也说过…

四款5G版iPhone 12齐发,苹果股价却应声而跌

整理 | 郑丽媛、屠敏题图 | 东方IC来源 | CSDN&#xff08;CSDNnews&#xff09;真快&#xff0c;又见面了。北京时间 10 月 14 日凌晨 1 点&#xff0c;Apple 举办的新品发布会如约而至。今年有关 iPhone 新品的到来有些迟&#xff0c;好在「5G just got real」&#xff0c;万…

Linux编译器GCC的使用

嵌入式Linux编译器GCC的使用 1、GCC概述 作为自由软件的旗舰项目&#xff0c;Richard Stallman在十多年前刚开始写作GCC的时候&#xff0c;还只是仅仅把它当作一个C程序语言的编译器&#xff0c;GCC的意思也只是GNU C Compiler而已。 经过了这么多年的发展&#xff0c;GCC已经不…

jquery兼容IE和火狐下focus()事件

<input type"text" id"my" name"my" /> <script type"text/javascript">$("#my").focus(); </script> 上面的代码在IE下是没有任何问题的,但是不兼容FF,在FF没有反应解决办法:兼容写法 IE和FF下focus()事…

Access-Control-Allow-Origin这个header这个头不能设置通配符域名

这个header属性&#xff0c;要么设置为*&#xff0c;即任何域名来源都行&#xff0c;要么就只能设置为一个或多个&#xff0c;确定的域名&#xff0c;不能使用通配符域名转载于:https://www.cnblogs.com/abcbuzhiming/p/6478910.html

MySQL Xtrabackup备份和恢复

简介 Xtrabackup是由percona提供的mysql数据库备份工具&#xff0c;据官方介绍&#xff0c;这也是世界上惟一一款开源的能够对innodb和xtradb数据库进行热备的工具。特点&#xff1a;(1)备份过程快速、可靠&#xff1b;(2)备份过程不会打断正在执行的事务&#xff1b;(3)能够基…

​吐血整理:手拿几个大厂offer的秘密武器!

怎样才能拿到大厂的offer&#xff1f;没有掌握绝对的技术&#xff0c;那么就要不断的学习。如何拿下阿里等大厂的offer呢&#xff0c;今天分享一个秘密武器&#xff0c;资深架构师整理的Java核心知识点&#xff0c;面试时面试官必问的知识点&#xff0c;篇章包括了很多知识点&a…

.NET中获取电脑名,IP地址,当前用户

在.NET中获取一台电脑名&#xff0c;IP地址及当前用户名是非常简单&#xff0c;以下是我常用的几种方法,如果大家还有其它好的方法&#xff0c;可以回复一起整理&#xff1a; 1. 在ASP.NET中专用属性&#xff1a; 获取服务器电脑名: Page.Server.ManchineName 获取用户信息:…

SpringMVC注解整理

2019独角兽企业重金招聘Python工程师标准>>> 使用注解之前要开启自动扫描功能 其中base-package为需要扫描的包(含子包)。 <context:component-scan base-package"cn.test"/> Configuration把一个类作为一个IoC容器&#xff0c;它的某个方法头上如果…

Facebook是如何做搜索的?

作者 | 一块小蛋糕来源 | NewBeeNLP今天要和大家分享的论文是来自Facebook的『Embedding based Retrieval in Facebook Search』。不得不说&#xff0c;F家的文章还是一如既往浓浓的工业风&#xff0c;这篇论文从工程角度讲解了一个召回的全流程&#xff0c;不管是做语义信息检…

JavaScript[对象.属性]集锦

作者&#xff1a; 蓝色理想 SCRIPT 标记? 用于包含JavaScript代码.? 属性? LANGUAGE 定义脚本语言? SRC 定义一个URL用以指定以.JS结尾的文件? windows对象? 每个HTML文档的顶层对象.? 属性? frames[] 子桢数组.每个子桢数组按源文档中定义的顺序存放.? feames…

c++ hook 钩子的使用介绍

一、基本概念&#xff1a; 钩子(Hook)&#xff0c;是Windows消息处理机制的一个平台,应用程序可以在上面设置子程以监视指定窗口的某种消息&#xff0c;而且所监视的窗口可以是其他进程所创建的。当消息到达后&#xff0c;在目标窗口处理函数之前处理它。钩子机制允许应用程序截…

小程序一次性上传多个本地图片,上拉加载照片以及图片加载延迟解决之道

一&#xff1a;小程序之一次性上传多个本地相片 最近由于项目需要所以学了下小程序&#xff0c;也做了一些东西&#xff0c;随后便有了以下的一些总结了&#xff0c;现在说说如何使用小程序一次性上传多个本地相片。 问题描述 最近做项目的时候要实现一个上传相片的功能&#x…

测试项目案例思路

近期帮公司培训部设计测试方向教学案例&#xff0c;原型为我们部门开发的某问卷系统&#xff0c;详情如下&#xff1a; 《**问卷系统》计划授课小时总数为85小时&#xff0c;预计实际授课要根据学生的掌握情况&#xff0c;建议增加5小时&#xff0c;请将此因素考虑到案例使用时…

赠书 | Python人脸五官姿态检测

作者 | 李秋键 出品 | AI科技大本营近几个月来由于疫情的影响使得网络授课得到了快速的发展&#xff0c;人工智能作为最有潜力的发展行业&#xff0c;同样可以应用于网络授课的监督。比如通过检测人脸姿态&#xff0c;眼睛是否张开&#xff0c;鼻子嘴巴等特征&#xff0c;来达到…

明白了这十个故事,你也就参悟了人生

1、断箭   不相信自己的意志&#xff0c;永远也做不成将军。   春秋战国时代&#xff0c;一位父亲和他的儿子出征打仗。父亲已做了将军&#xff0c;儿子还只是马前卒。又一阵号角吹响&#xff0c;战鼓雷鸣了&#xff0c;父亲庄严地托起一个箭囊&#xff0c;其中插着一只箭。…