using System;

using System.Collections;

using System.Collections.Generic;

using System.DirectoryServices;

using System.Linq;

using System.Net;

using System.Text;

using System.Threading.Tasks;

namespace IISControlHelper

{

/// <summary>

/// IIS 操作方法集合

/// http://blog.csdn.net/ts1030746080/article/details/8741399 错误

/// </summary>

public class IISWorker

{

private static string HostName = "localhost";

/// <summary>

/// 获取本地IIS版本

/// </summary>

/// <returns></returns>

public static string GetIIsVersion()

{

try

{

DirectoryEntry entry = new DirectoryEntry("IIS://" + HostName + "/W3SVC/INFO");

string version = entry.Properties["MajorIISVersionNumber"].Value.ToString();

return version;

}

catch (Exception se)

{

//说明一点:IIS5.0中没有(int)entry.Properties["MajorIISVersionNumber"].Value;属性,将抛出异常 证明版本为 5.0

return string.Empty;

}

}

/// <summary>

/// 创建虚拟目录网站

/// </summary>

/// <param name="webSiteName">网站名称</param>

/// <param name="physicalPath">物理路径</param>

/// <param name="domainPort">站点+端口,如192.168.1.23:90</param>

/// <param name="isCreateAppPool">是否创建新的应用程序池</param>

/// <returns></returns>

public static int CreateWebSite(string webSiteName, string physicalPath, string domainPort,bool isCreateAppPool)

{

DirectoryEntry root = new DirectoryEntry("IIS://" + HostName + "/W3SVC");

// 为新WEB站点查找一个未使用的ID

int siteID = 1;

foreach (DirectoryEntry e in root.Children)

{

if (e.SchemaClassName == "IIsWebServer")

{

int ID = Convert.ToInt32(e.Name);

if (ID >= siteID) { siteID = ID + 1; }

}

}

// 创建WEB站点

DirectoryEntry site = (DirectoryEntry)root.Invoke("Create", "IIsWebServer", siteID);

site.Invoke("Put", "ServerComment", webSiteName);

site.Invoke("Put", "KeyType", "IIsWebServer");

site.Invoke("Put", "ServerBindings", domainPort + ":");

site.Invoke("Put", "ServerState", 2);

site.Invoke("Put", "FrontPageWeb", 1);

site.Invoke("Put", "DefaultDoc", "Default.html");

// site.Invoke("Put", "SecureBindings", ":443:");

site.Invoke("Put", "ServerAutoStart", 1);

site.Invoke("Put", "ServerSize", 1);

site.Invoke("SetInfo");

// 创建应用程序虚拟目录

DirectoryEntry siteVDir = site.Children.Add("Root", "IISWebVirtualDir");

siteVDir.Properties["AppIsolated"][0] = 2;

siteVDir.Properties["Path"][0] = physicalPath;

siteVDir.Properties["AccessFlags"][0] = 513;

siteVDir.Properties["FrontPageWeb"][0] = 1;

siteVDir.Properties["AppRoot"][0] = "LM/W3SVC/" + siteID + "/Root";

siteVDir.Properties["AppFriendlyName"][0] = "Root";

if (isCreateAppPool)

{

DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");

DirectoryEntry newpool = apppools.Children.Add(webSiteName, "IIsApplicationPool");

newpool.Properties["AppPoolIdentityType"][0] = "4"; //4

newpool.Properties["ManagedPipelineMode"][0] = "0"; //0:集成模式 1:经典模式

newpool.CommitChanges();

siteVDir.Properties["AppPoolId"][0] = webSiteName;

}

siteVDir.CommitChanges();

site.CommitChanges();

return siteID;

}

/// <summary>

/// 得到网站的物理路径

/// </summary>

/// <param name="rootEntry">网站节点</param>

/// <returns></returns>

public static string GetWebsitePhysicalPath(DirectoryEntry rootEntry)

{

string physicalPath = "";

foreach (DirectoryEntry childEntry in rootEntry.Children)

{

if ((childEntry.SchemaClassName == "IIsWebVirtualDir") && (childEntry.Name.ToLower() == "root"))

{

if (childEntry.Properties["Path"].Value != null)

{

physicalPath = childEntry.Properties["Path"].Value.ToString();

}

else

{

physicalPath = "";

}

}

}

return physicalPath;

}

/// <summary>

/// 获取站点名

/// </summary>

public static List<IISInfo> GetServerBindings()

{

List<IISInfo> iisList = new List<IISInfo>();

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

DirectoryEntry ent = new DirectoryEntry(entPath);

foreach (DirectoryEntry child in ent.Children)

{

if (child.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase))

{

if (child.Properties["ServerBindings"].Value != null)

{

object objectArr = child.Properties["ServerBindings"].Value;

string serverBindingStr = string.Empty;

if (IsArray(objectArr))//如果有多个绑定站点时

{

object[] objectToArr = (object[])objectArr;

serverBindingStr = objectToArr[0].ToString();

}

else//只有一个绑定站点

{

serverBindingStr = child.Properties["ServerBindings"].Value.ToString();

}

IISInfo iisInfo = new IISInfo();

iisInfo.DomainPort = serverBindingStr;

iisInfo.AppPool = child.Properties["AppPoolId"].Value.ToString();//应用程序池

iisList.Add(iisInfo);

}

}

}

return iisList;

}

public static bool CreateAppPool(string appPoolName, string Username, string Password)

{

bool issucess = false;

try

{

//创建一个新程序池

DirectoryEntry newpool;

DirectoryEntry apppools = new DirectoryEntry("IIS://" + HostName + "/W3SVC/AppPools");

newpool = apppools.Children.Add(appPoolName, "IIsApplicationPool");

//设置属性 访问用户名和密码 一般采取默认方式

newpool.Properties["WAMUserName"][0] = Username;

newpool.Properties["WAMUserPass"][0] = Password;

newpool.Properties["AppPoolIdentityType"][0] = "3";

newpool.CommitChanges();

issucess = true;

return issucess;

}

catch // (Exception ex)

{

return false;

}

}

/// <summary>

/// 建立程序池后关联相应应用程序及虚拟目录

/// </summary>

public static void SetAppToPool(string appname,string poolName)

{

//获取目录

DirectoryEntry getdir = new DirectoryEntry("IIS://localhost/W3SVC");

foreach (DirectoryEntry getentity in getdir.Children)

{

if (getentity.SchemaClassName.Equals("IIsWebServer"))

{

//设置应用程序程序池 先获得应用程序 在设定应用程序程序池

//第一次测试根目录

foreach (DirectoryEntry getchild in getentity.Children)

{

if (getchild.SchemaClassName.Equals("IIsWebVirtualDir"))

{

//找到指定的虚拟目录.

foreach (DirectoryEntry getsite in getchild.Children)

{

if (getsite.Name.Equals(appname))

{

//【测试成功通过】

getsite.Properties["AppPoolId"].Value = poolName;

getsite.CommitChanges();

}

}

}

}

}

}

}

/// <summary>

/// 判断object对象是否为数组

/// </summary>

public static bool IsArray(object o)

{

return o is Array;

}

}

}

C# 操作IIS服务器Demo相关推荐

  1. c# 操作服务器虚拟目录,C# 操作IIS服务器Demo

    原标题:C# 操作IIS服务器Demo using System; using System.Collections; using System.Collections.Generic; using ...

  2. 启用本地IIS服务器操作

    启用本地IIS服务器操作 打开电脑上的[控制面板]–[程序],在[程序和功能]中选择[打开或关闭Windows功能]. 按照下图所示,勾上对应的选项框,打开万维网服务的加号,也是需要全部勾选的.选择完 ...

  3. nginx与IIS服务器搭建集群实现负载均衡(二)

    强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan [前言] 在<架构之路:nginx与IIS服务器搭建集群实现负载均衡(一)>中小编简 ...

  4. tomcat与IIS服务器集成

    22.4 Tomcat与IIS服务器集成 IIS(Internet Information Service)服务器是微软开发的功能强大的Web服务器,IIS为创建和开发电子商务的提供了安全的Web平台 ...

  5. 搭建服务器集群——Windows7系统中nginx与IIS服务器搭建集群实现负载均衡

    转载:https://www.cnblogs.com/xiongze520/p/10308720.html 分布式,集群,云计算机.大数据.负载均衡.高并发······当耳边响起这些词时,做为一个菜鸟 ...

  6. HTTP 错误 403.9 - 禁止访问:连接的用户过多 XP IIS服务器连接数的修改

    计算机教室的机子是XP的,用IIS很不爽,学生机连接到教师机下载资料或上交作业时常常遇到连接的用户过多的错误提示(HTTP 错误 403.9 - 禁止访问:连接的用户过多),这是因为Windows X ...

  7. 生成pfx文件需要在服务器上执行,PEM文件和private.key文件生成IIS服务器所需的pfx文件(配置SSL用)...

    第一步:输入域名,点击"创建免费的SSL证书"按钮,如图 第二步:输入邮箱,点击创建,如图 创建成功后,需要进行dns验证,结果图如下: 第三步:在域名所在的云服务器上,添加域名解 ...

  8. Net中如何操作IIS

    Net中实际上已经为我们在这方面做得很好了.FCL中提供了不少的类来帮助我们完成这项工作,让我们的开发工作变非常简单和快乐.编程控制IIS实际上很简单,和ASP一样,.Net中需要使用ADSI来操作I ...

  9. asp.net操作IIS主机头的问题总结

    今天发现一个怪怪的问题,在程序自动操作IIS主机头的问题上,在本地测试时没有任何问题,新增,删除主机头都正常.但传到服务器上时就有问题了,在新增主机头时没问题,删除主机头时就出现了这样的提示:拒绝访问 ...

最新文章

  1. FragmentPagerAdapter 与 FragmentStatePagerAdapter 的区别
  2. mysql中怎样扑抓到是那个字段出错_mysql 常见的几个错误问题
  3. 利用sqoop将hive数据导入导出数据到mysql
  4. java selenium 日志_java - 支持selenium日志_java_酷徒编程知识库
  5. Spring boot actuator端点启用和暴露
  6. python相对引用_Python 模块相对引用
  7. MySQL-->索引-->如何创建索引,创建原则
  8. Mac精品软件分享第二期
  9. HDU 3790 最短路径问题 (SPFA)
  10. WebStorm学习笔记004---Webstorm的一些常用快捷键
  11. 解剖SQLSERVER 第八篇 OrcaMDF 现在支持多数据文件的数据库(译)
  12. SPSS缺失值处理【SPSS 009期】
  13. 极客大学产品经理训练营 产品文档和原型 作业5
  14. 【华为OD机试Python实现】HJ70 矩阵乘法计算量估算(中等)
  15. cf 1487C - Minimum Ties
  16. 实现美团、饿了么购物车效果,并本地存储相关数据
  17. 什么是gnuplot
  18. CyclicBarrier: 循环栅栏
  19. 相约AIIA!百度飞桨深度学习公开课预约中
  20. 非诚勿扰php男嘉宾,非诚勿扰 php

热门文章

  1. halcon get_image_pointer1获取图像指针
  2. HALCON示例程序measure_circles.hdev测量圆的半径
  3. 学习笔记(38):Python实战编程-窗体显示
  4. 键盘改键软件_一秒五键,一键三招,万种光污染,杜伽K310樱桃轴机械键盘感受...
  5. python中的列表,添加元素,获取元素,删除元素,列表分片,常用操作符
  6. 石油采集(求联通区域) 2018多校寒假集训 (dfs+二分匹配)
  7. poj1743 Musical Theme
  8. C#基础之Equals和Dispose
  9. UVA - 11732 strcmp() Anyone?左兄弟右儿子trie
  10. rebuild online意外终止导致ora-8104错误的实验