原文: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;

转载于:https://www.cnblogs.com/erictanghu/p/3761019.html

C#使用DirectoryEntry操作IIS创建网站和虚拟路径相关推荐

  1. C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585)

    在使用C#操作IIS创建应用程序池出现异常:无效索引(Exception from HRESULT:0x80070585) 相关代码: public static string CreateAppPo ...

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

    C#操作IIS程序池及站点的创建配置(转) 原文:http://www.cnblogs.com/wujy/archive/2013/02/28/2937667.html 最近在做一个WEB程序的安装包 ...

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

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

  4. C#操作IIS站点 Microsoft.Web.Administration.dll

    利用IIS7自带类库管理IIS现在变的更强大更方便,而完全可以不需要用DirecotryEntry这个类了(网上很多.net管理iis6.0的文章都用到了DirecotryEntry这个类 ),Mic ...

  5. Net中如何操作IIS

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

  6. C# 操作IIS服务器Demo

    using System; using System.Collections; using System.Collections.Generic; using System.DirectoryServ ...

  7. 小白创建网站的曲折之路

    小白创建网站的曲折之路 在虚拟机上创建网站 顾名思义,首先要有一个虚拟机.在网上百度一下后,我发现大家都在说使用一种叫做VMware Workstation的软件进行虚拟机的构建 在这位好心人的帮助下 ...

  8. 将远程计算机上的文件夹,如何将现有网站上虚拟目录创建到驻留在远程计算机上的文件夹...

    如何将现有网站上虚拟目录创建到驻留在远程计算机上的文件夹 09/27/2020 本文内容 本文介绍如何将现有网站上虚拟目录创建.测试和删除到驻留在远程计算机上的文件夹. 适用于:  Windows S ...

  9. C#配置IIS搭建网站的工具类

    public class IISWorker{public static string HostName = "localhost";/// <summary>/// ...

最新文章

  1. 清华张亚勤院士团队招聘 AI 工业方向博士后
  2. 配置文件占位符||Profile——1、多Profile文件 2、yml支持多文档块方式 3、激活指定profile
  3. MySQL为关联表添加数据
  4. LeetCode 1598. 文件夹操作日志搜集器
  5. jtessboxeditorfx 界面显示不出来_华为Mate40 Pro开箱简评,有点不开心
  6. 优化Meta讨好搜索引擎 更好的提升网站排名
  7. php一点通,编程一点通
  8. 博客园山寨版(asp.net mvc 开源)
  9. mysql选择产品和功能_mysql - 产品属性选择
  10. Jackson安全漏洞版本升级
  11. python导入random模块_python random模块(随机数)详解
  12. java后端通过Filter过滤器解决跨域问题
  13. vs-code-多设备插件同步插件Settings Sync
  14. 360手机浏览器_UC、QQ、华为、360、搜狗、小米、vivo、OPPO等8款手机浏览器被纳入首批传播秩序专项整治...
  15. 慎重选择博士后(或博士生)导师
  16. 研究生如何参加以及准备学术会议详细攻略-9000字
  17. es文件创建局域网服务器,es文件浏览器局域网连接win10电脑怎么设置
  18. Contrastive Test-Time Adaptation
  19. solidworks拉伸凸台基体/基体
  20. Python-猫耳MF

热门文章

  1. zend studio php 错误提示,如何解决Win7打开启动ZendStudio PHP时提示错误
  2. Python中文文本聚类
  3. jvm初学-什么是Native方法
  4. stata代码乱码、转码问题的语句
  5. abaqus java 二次开发_Abaqus二次开发介绍
  6. Abaqus常用命令
  7. c# 超时时间已到.在操作完成之前超时时间已过或服务器未响应,c#执行插入sql 时,报错:异常信息:超时时间已到。在操作完成之前超时时间已过或服务器未响应...
  8. 信息学奥赛训练体系(2023.02.21)
  9. vs2015 + BabeLua + Cocos2d-x 3.10配置
  10. 图像边缘锯齿及处理方法