原文:http://www.cnblogs.com/Aiooioo/archive/2011/05/30/cs-iis.html

在.Net中我们可以使用内置的类DirectoryEntry来承载IIS服务器中的任何网站,虚拟路径或应用程序池对象,例如:
 
DirectoryEntry ent = new DirectoryEntry("IIS://localhost/w3svc/1/root");
就创建了一个IIS路径为IIS://localhost/w3svc/1/root的虚拟路径对象。
 
为了在IIS中创建一个网站,我们首先需要确定输入的网站路径在IIS中是否存在,这里主要是根据网站在IIS中的ServerBindings属性来区分:
DirectoryEntry ent;
DirectoryEntry rootEntry;
try
{
  ent = EnsureNewWebSiteAvailable(host + ":" + port + ":" + webSiteDesc);
  if (ent != null)
  {
    //这里如果用户输入的网站在IIS中已经存在,那么直接获取网站的root对象,也就是网站的默认应用程序
    rootEntry = ent.Children.Find("root", "IIsWebVirtualDir");
  }
  else
  {
    //如果网站在IIS不存在,那么我们需要首先在IIS中创建该网站,并且为该网站创建一个root应用程序
    string entPath = string.Format("IIS://{0}/w3svc", Host);
    DirectoryEntry root = GetDirectoryEntry(entPath);
    string newSiteNum = GetNewWebSiteID();
    DirectoryEntry newSiteEntry = root.Children.Add(newSiteNum, "IIsWebServer");
    newSiteEntry.CommitChanges();
    newSiteEntry.Properties["ServerBindings"].Value = host + ":" + port + ":" + webSiteDesc;
    newSiteEntry.Properties["ServerComment"].Value = webSiteComment;
    newSiteEntry.CommitChanges();
    rootEntry = newSiteEntry.Children.Add("root", "IIsWebVirtualDir");
    rootEntry.CommitChanges();
    rootEntry.Properties["Path"].Value = webSitePath;
    rootEntry.Properties["AppPoolId"].Value = appPool;
    rootEntry.Properties["AccessRead"][0] = true; // 勾选读取
    rootEntry.Properties["AuthFlags"][0] = 1+4; 
    //勾选匿名访问和windows身份验证
    /** 标志
    标志名AuthBasic
    描述指定基本身份验证作为可能的 
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_BASIC
    十进制值2
    十六进制值0x00000002
    
    标志名AuthAnonymous
    描述指定匿名身份验证作为可能的 
    Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_ANONYMOUS
    十进制值1
    十六进制值0x00000001
    
    标志名AuthNTLM
    描述指定集成 Windows 
    身份验证(也称作质询/响应或 NTLM 验证)作为可能的 Windows 验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_NT
    十进制值4
    十六进制值0x00000001
 
    标志名AuthMD5
    描述指定摘要式身份验证和高级摘要式身份验证作为可能的 Windows 
    验证方案之一,返回给客户端作为有效验证方案。
    配置数据库位掩码标识符MD_AUTH_MD5
    十进制值16
    十六进制值0x00000010
    
    标志名AuthPassport
    描述true 的值表示启用了 Microsoft .NET Passport 身份验证。 详细信息,请参阅 .NET Passport 验证。
    配置数据库位掩码标识符MD_AUTH_PASSPORT
    十进制值64
    十六进制值0x00000040
    */
    rootEntry.Properties["DontLog"][0] = true;
    rootEntry.Properties["AuthAnonymous"][0] = true;
    rootEntry.Properties["AnonymousUserName"][0] =
    XmlSettings.GetWebXmlSettingString("IISAnonymousUserName");
    
    /*这里AnonymousUserPass属性如果不去设置,IIS会自动控制匿名访问账户的密码。之前我尝试将匿名访问用户的密码传给网站,之后发现创建出来的网站尽管勾选的匿名访问并且设置了匿名用户密码,浏览的时候还是提示要输入密码,很是纠结*/
    rootEntry.Invoke("AppCreate", true);
    rootEntry.CommitChanges();
  }
  DirectoryEntry de = rootEntry.Children.Add(friendlyName, rootEntry.SchemaClassName);
  de.CommitChanges();
  de.Properties["Path"].Value = virtualPath;
  de.Properties["AccessRead"][0] = true; // 勾选读取
  de.Invoke("AppCreate", true);
  de.Properties["EnableDefaultDoc"][0] = true;
  de.Properties["AccessScript"][0] = true; // 脚本资源访问
  de.Properties["DontLog"][0] = true; // 勾选记录访问
  de.Properties["ContentIndexed"][0] = true; // 勾选索引资源
  de.Properties["AppFriendlyName"][0] = friendlyName; //应用程序名
  de.Properties["AuthFlags"][0] = 5;
  /*这里在创建虚拟路径时不需要再次设置匿名访问,因为网站下的虚拟路径会默认接受网站的访问限制设置*/
  de.CommitChanges();
}
catch (Exception e)
{
  throw e;
}
 
public string GetNewWebSiteID()
{
  ArrayList list = new ArrayList();
  string tempStr;
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  DirectoryEntry ent = GetDirectoryEntry(entPath);
  foreach (DirectoryEntry child in ent.Children)
  {
    if (child.SchemaClassName == "IIsWebServer")
    {
      tempStr = child.Name.ToString();
      list.Add(Convert.ToInt32(tempStr));
    }
  }
  list.Sort();
  var newId = Convert.ToInt32(list[list.Count - 1]) + 1;
  return newId.ToString();
}
 
public DirectoryEntry GetDirectoryEntry(string entPath)
{
  DirectoryEntry ent;
  if (string.IsNullOrEmpty(UserName))
  {
    ent = new DirectoryEntry(entPath);
  }
  else
  {
    ent = new DirectoryEntry(entPath, Host + "\\" + UserName, Password, AuthenticationTypes.Secure);
  }
  return ent;
}
 
public DirectoryEntry EnsureNewWebSiteAvailable(string bindStr)
{
  string entPath = string.Format("IIS://{0}/w3svc",Host);
  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 child; }
      }
    }
  }
  return null;

C#使用DirectoryEntry操作IIS创建网站和虚拟路径的更多相关文章

  1. 利用iis创建网站后为什么不能设置主机名

    主机名 主机名就是网站的域名,通俗说就是网站地址(如:www.baidu.com). 设置了主机名,而IIS确不知道主机名对应的地址在哪里. 举个例子,把www.baidu.com做为IIS网站的主机 ...

  2. 用C#操作IIS创建虚拟目录和网站

    #region CreateWebsite 添加网站 public string CreateWebSite(string serverID, string serverComment, string ...

  3. windows下利用iis建立网站网站并实现局域共享

    博客园 首页 新随笔 联系 管理 订阅 随笔- 54  文章- 9  评论- 0  Windows下利用IIS建立网站并实现局域网共享 https://blog.csdn.net/qq_4148541 ...

  4. Windows下利用IIS建立网站并实现局域网共享

    https://blog.csdn.net/qq_41485414/article/details/82754252 https://www.cnblogs.com/linuxprobe-sarah/ ...

  5. C#操作IIS程序池及站点的创建配置

    最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主要包括对IIS进行站点的新建以及新建站点的NET版本的选择,还有针对IIS7程序池的托管模式以及版本的操作:首先要对Microso ...

  6. C#操作IIS程序池及站点的创建配置(转)

      原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包:对一些操作IIS进行一个简单的总结:主 ...

  7. C#操作IIS程序池及站点的创建配置实现代码

    首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...

  8. .net操作IIS,新建网站,新建应用程序池,设置应用程序池版本,设置网站和应用程序池的关联

    ServerManager类用来操作IIS,提供了很多操作IIS的API.使用ServerManager必须引用Microsoft.Web.Administration.dll,具体路径为:%wind ...

  9. c#操作IIS站点

    /// <summary> /// 获取本地IIS版本 /// </summary> /// <returns></returns> public st ...

随机推荐

  1. OOM总结

    本文主要信息是来自互联网,我只是自己做了一点总结和摘要. OOM发生的原因 简单的说通过不同的内存分配方式对不同的对象进行操作,会因为android系统版本的差异而产生不同的行为.主要是2.0和4.0 ...

  2. 华为 oj 公共子串计算

    水题,原来以为用dp数组  结果wrong了两次 我想还是自己小题大做了···呵呵·· 献给初学者作为参考 #include <stdio.h> #include <string.h ...

  3. C#按键打开文件选择对话框,并把选择好的路径保存/显示到textBox

    1.选择文件夹 FolderBrowserDialog fbd = new FolderBrowserDialog(); fbd.SelectedPath = "D:";//默认路 ...

  4. VC连接数据库方式

    转自:http://www.cnblogs.com/renyuan/archive/2012/07/27/2612412.html 目前Windows系统上常见的数据库接口包括: ODBC(开放数据库 ...

  5. CC2530定时器1的模模式中断

    CC2530定时器1的模模式中断void timer1SInit(void){ T1CCTL0 = 0; T1CTL &= ~0x0F; //clear register T1CTL |= 0 ...

  6. 解决位于底端Edittext 输入时被软盘遮盖

    遇到这种情况我们首先到网上搜一圈,大概情况是需要设置<activity/> android:windowSoftInputMode="adjustResize" ,按照 ...

  7. http、TCP/IP协议与socket之间的区别

    http.TCP/IP协议与socket之间的区别     网络由下往上分为:  www.2cto.com   物理层--                       数据链路层-- 网络层--   ...

  8. ubuntu vim YCM

    http://blog.sina.com.cn/s/blog_499386b00100rxm1.html http://www.cnblogs.com/junnyfeng/p/3633697.html

  9. LeetCode_N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...

  10. IC封装图片认识(一):BGA

    在上篇文章<常用IC封装技术介绍>第一个提到的IC封装形式就是BGA,全称是Ball Grid Array(球栅阵列结构的PCB),它是集成电路采用有机载板的一种封装法.其具有以下五个特点 ...