如何通过代码来创建IIS虚拟目录 如何通过代码来创建IIS虚拟目录 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.cnblogs.com/newsea/archive/2008/08/28/1278766.html using System.IO;using System.DirectoryServices;// ..... /// <summary> /// 创建iis虚拟目录 /// </summary> /// <param name="physicalPath">虚拟目录所指向的物理路径</param> /// <param name="virtualDirName">虚拟目录名称</param> private void CreateDirectory(string physicalPath, string virtualDirName) { if (!DirectoryEntry.Exists("IIS://localhost/W3SVC/1/ROOT" + "\\" + virtualDirName)) { try { DirectoryEntry root = new DirectoryEntry(IISWebSiteRoot); DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName); tbEntry.Properties["Path"][0] = physicalPath; //设置虚拟目录指向的物理路径 tbEntry.Invoke("AppCreate", true); tbEntry.Properties["AccessRead"][0] = true; //设置读取权限 // tbEntry.Properties["AccessRead"][0] = false; tbEntry.Properties["ContentIndexed"][0] = true; tbEntry.Properties["DefaultDoc"][0] = "Index.aspx,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割 tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; //应用程序名称 tbEntry.Properties["AccessScript"][0] = true; //执行权限 tbEntry.Properties["DontLog"][0] = true; tbEntry.Properties["AuthFlags"][0] = 0; //设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证 tbEntry.Properties["AuthFlags"][0] = 1; tbEntry.CommitChanges(); } catch (Exception ex) { Respone.write("<script>alert('" + ex.Message + '');<script>'); } } } 转自:http://www.cnblogs.com/libiyang/archive/2009/01/04/294455.html如何使用DirectoryEntry来实现IIS虚拟目录的管理1、创建虚拟目录:创建虚拟目录的经过以下步骤:1)获取该虚拟目录的上级目录的DirectoryEntry对象rootEntry;2)通过rootEntry的DirectoryEntry::Childrens.Add来添加该虚拟目录;// 创建虚拟目录 DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");3)更新该虚拟目录的属性,如更新身份验证模式,访问权限和所对应的物理目录等。需要注意的是,使用DirectoryEntry来创建虚拟目录,只能在该虚拟目录建立了以后,才能设置物理目录等属性。创建虚拟目录的代码如下: 1 /// <summary> 2 /// 创建iis虚拟目录 3 /// </summary> 4 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception> 5 public override void CreateDirectory() 6 { 7 // 已不覆盖的方式创建虚拟目录,当虚拟目录存在时抛出异常 8 this.CreateDirectory(false); 9 }10 11 /// <summary>12 /// 创建iis虚拟目录13 /// </summary>14 /// <param name="bReplace">是否覆盖掉原有的虚拟目录</param>15 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>16 public override void CreateDirectory(bool bReplace)17 {18 // 判断目录是否存在19 if (this.Exist())20 {21 if (bReplace)22 {23 // 若允许覆盖则先删除原有的虚拟目录24 this.DeleteDirectory();25 }26 else27 {28 // 若不允许覆盖直接抛出目录已存在的异常29 DirectoryException.Throw("directory already exist");30 }31 }32 33 try34 {35 // 获取上级目录的DirectoryEntry对象36 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);37 // 创建虚拟目录38 DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");39 entry.Invoke("AppCreate", true);40 entry.CommitChanges();41 rootEntry.CommitChanges();42 43 // 更新虚拟目录属性44 SystemDirectory.UpdateEntry(entry, this.Property);45 }46 catch (System.Exception ex)47 {48 DirectoryException.Throw(ex.Message);49 }50 }DirectoryEntry 的commitChanges方法用于提交DirectoryEntry的操作。line41中的提交了以后,该虚拟目录才能建立,然后在进行更新虚拟目录的属性的操作。若没有commitChanges就进行更新操作,会抛出找不到虚拟目录的异常,有兴趣的朋友可以试试。2、删除虚拟目录:删除虚拟目录比较简单,只需调用虚拟目录的父节点的DirectoryEntry对象的Delete操作就可以了,需要注意的是Delete对象需要两个参数:子节点的Alias和子节点的类型(虚拟目录节点的类型为IIsWebVirtualDir)。 1 /// <summary> 2 /// 删除iis虚拟目录 3 /// </summary> 4 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception> 5 public override void DeleteDirectory() 6 { 7 // 判断目录是否存在 8 if (! this.Exist()) 9 {10 // 若待删除的虚拟目录不存在,则抛出异常11 DirectoryException.Throw("directory does not exist");12 }13 14 try15 {16 // 获取上级目录的DirectoryEntry对象17 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);18 // 删除参数19 object[] objParams = new object[2];;20 objParams[0] = "IIsWebVirtualDir";21 objParams[1] = this.m_strAlias;22 // 删除虚拟目录23 rootEntry.Invoke("Delete", objParams);24 rootEntry.CommitChanges();25 }26 catch (System.Exception ex)27 {28 DirectoryException.Throw(ex.Message);29 }30 31 }3、判断虚拟目录是否存在:通过调用父节点的DirectoryEntry对象的DirectoryEntry::Children.Find来查找子结点是否存在,但非常奇怪的当子节点不存在的时候会抛出异常,我更期望的是返回子节点的null引用。 1 /// <summary> 2 /// 判断iis虚拟目录是否存在 3 /// </summary> 4 /// <returns>目录是否存在</returns> 5 public override bool Exist() 6 { 7 bool bExist = false; 8 9 try10 {11 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);12 DirectoryEntry entry = rootEntry.Children.Find(this.Alias, "IIsWebVirtualDir");13 bExist = (entry != null);14 }15 catch{}16 17 return bExist;18 } 上面的很详细了吧不知道你ADSI听说没 radio 弹出新的窗口问题? 关于多个文件上传 两个关于Treeview的问题 谁有TCP/IP协议的资料 .NET开发的 100分,请问datagrid分页还要写代码吗?在线等,急 需要读取数据库里面的值生成TreeView,树是IE Web Control里面的TreeView 编译正常.发布就报错.类型同时存在..急!!!急!!!急!!! 页面传值的问题... ADO.NET的问题? vss2005无法获取最新版本 帮个忙 谢谢 关于ASP.NET SQL SERVER 的查询语句
using System.IO;
using System.DirectoryServices;// ..... /// <summary>
/// 创建iis虚拟目录
/// </summary>
/// <param name="physicalPath">虚拟目录所指向的物理路径</param>
/// <param name="virtualDirName">虚拟目录名称</param>
private void CreateDirectory(string physicalPath, string virtualDirName)
{
if (!DirectoryEntry.Exists("IIS://localhost/W3SVC/1/ROOT" + "\\" + virtualDirName))
{
try
{
DirectoryEntry root = new DirectoryEntry(IISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualDirName, root.SchemaClassName); tbEntry.Properties["Path"][0] = physicalPath; //设置虚拟目录指向的物理路径
tbEntry.Invoke("AppCreate", true); tbEntry.Properties["AccessRead"][0] = true; //设置读取权限 // tbEntry.Properties["AccessRead"][0] = false;
tbEntry.Properties["ContentIndexed"][0] = true;
tbEntry.Properties["DefaultDoc"][0] = "Index.aspx,Default.aspx"; //设置默认文档,多值情况下中间用逗号分割
tbEntry.Properties["AppFriendlyName"][0] = virtualDirName; //应用程序名称
tbEntry.Properties["AccessScript"][0] = true; //执行权限
tbEntry.Properties["DontLog"][0] = true;
tbEntry.Properties["AuthFlags"][0] = 0; //设置目录的安全性,0表示不允许匿名访问,1为允许,3为基本身份验证,7为windows继承身份验证
tbEntry.Properties["AuthFlags"][0] = 1;
tbEntry.CommitChanges();
}
catch (Exception ex)
{
Respone.write("<script>alert('" + ex.Message + '');<script>');
}
}
}
如何使用DirectoryEntry来实现IIS虚拟目录的管理1、创建虚拟目录:
创建虚拟目录的经过以下步骤:
1)获取该虚拟目录的上级目录的DirectoryEntry对象rootEntry;
2)通过rootEntry的DirectoryEntry::Childrens.Add来添加该虚拟目录;
// 创建虚拟目录
DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");
3)更新该虚拟目录的属性,如更新身份验证模式,访问权限和所对应的物理目录等。需要注意的是,使用DirectoryEntry来创建虚拟目录,只能在该虚拟目录建立了以后,才能设置物理目录等属性。创建虚拟目录的代码如下:
1 /// <summary>
2 /// 创建iis虚拟目录
3 /// </summary>
4 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
5 public override void CreateDirectory()
6 {
7 // 已不覆盖的方式创建虚拟目录,当虚拟目录存在时抛出异常
8 this.CreateDirectory(false);
9 }
10
11 /// <summary>
12 /// 创建iis虚拟目录
13 /// </summary>
14 /// <param name="bReplace">是否覆盖掉原有的虚拟目录</param>
15 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
16 public override void CreateDirectory(bool bReplace)
17 {
18 // 判断目录是否存在
19 if (this.Exist())
20 {
21 if (bReplace)
22 {
23 // 若允许覆盖则先删除原有的虚拟目录
24 this.DeleteDirectory();
25 }
26 else
27 {
28 // 若不允许覆盖直接抛出目录已存在的异常
29 DirectoryException.Throw("directory already exist");
30 }
31 }
32
33 try
34 {
35 // 获取上级目录的DirectoryEntry对象
36 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
37 // 创建虚拟目录
38 DirectoryEntry entry = rootEntry.Children.Add(this.m_strAlias, "IIsWebVirtualDir");
39 entry.Invoke("AppCreate", true);
40 entry.CommitChanges();
41 rootEntry.CommitChanges();
42
43 // 更新虚拟目录属性
44 SystemDirectory.UpdateEntry(entry, this.Property);
45 }
46 catch (System.Exception ex)
47 {
48 DirectoryException.Throw(ex.Message);
49 }
50 }
DirectoryEntry 的commitChanges方法用于提交DirectoryEntry的操作。line41中的提交了以后,该虚拟目录才能建立,然后在进行更新虚拟目录的属性的操作。若没有commitChanges就进行更新操作,会抛出找不到虚拟目录的异常,有兴趣的朋友可以试试。2、删除虚拟目录:
删除虚拟目录比较简单,只需调用虚拟目录的父节点的DirectoryEntry对象的Delete操作就可以了,需要注意的是Delete对象需要两个参数:子节点的Alias和子节点的类型(虚拟目录节点的类型为IIsWebVirtualDir)。
1 /// <summary>
2 /// 删除iis虚拟目录
3 /// </summary>
4 /// <exception cref="CreateIIsDirectory.DirectoryException">虚拟目录操作异常</exception>
5 public override void DeleteDirectory()
6 {
7 // 判断目录是否存在
8 if (! this.Exist())
9 {
10 // 若待删除的虚拟目录不存在,则抛出异常
11 DirectoryException.Throw("directory does not exist");
12 }
13
14 try
15 {
16 // 获取上级目录的DirectoryEntry对象
17 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
18 // 删除参数
19 object[] objParams = new object[2];;
20 objParams[0] = "IIsWebVirtualDir";
21 objParams[1] = this.m_strAlias;
22 // 删除虚拟目录
23 rootEntry.Invoke("Delete", objParams);
24 rootEntry.CommitChanges();
25 }
26 catch (System.Exception ex)
27 {
28 DirectoryException.Throw(ex.Message);
29 }
30
31 }3、判断虚拟目录是否存在:
通过调用父节点的DirectoryEntry对象的DirectoryEntry::Children.Find来查找子结点是否存在,但非常奇怪的当子节点不存在的时候会抛出异常,我更期望的是返回子节点的null引用。
1 /// <summary>
2 /// 判断iis虚拟目录是否存在
3 /// </summary>
4 /// <returns>目录是否存在</returns>
5 public override bool Exist()
6 {
7 bool bExist = false;
8
9 try
10 {
11 DirectoryEntry rootEntry = SystemDirectory.GetParentEntry(this);
12 DirectoryEntry entry = rootEntry.Children.Find(this.Alias, "IIsWebVirtualDir");
13 bExist = (entry != null);
14 }
15 catch{}
16
17 return bExist;
18 }
不知道你ADSI听说没