[英]Creating virtual directories in sub directories in IIS 6.0 (programmatically)
I am attempting to create virtual directories in IIS 6.0 programmatically and having problems checking if a virtual directory exists if the virtual directory is in a nested folder. 我试图以编程方式在IIS 6.0中创建虚拟目录,并且如果虚拟目录位于嵌套文件夹中,则无法检查虚拟目录是否存在问题。
So if I have a folder tree such as: 因此,如果我有一个文件夹树,例如:
MySite
Folder A (virtual directory)
Folder B
NestedFolder C (virtual directory)
When I grab the DirectoryEntry object for this site metabase: 当我获取此网站配置数据库的DirectoryEntry对象时:
"IIS://<servername>/W3SVC/2/Root"
The DirectoryEntry object (will call it entry) will have two children, with DirectoryEntry对象(将其称为条目)将具有两个子对象,其中
entry.Children[0].Name = "Folder A"
but entry.Children[1].Name = "Folder B" which is not a virtual directory. 但是entry.Children [1] .Name =“文件夹B”,它不是虚拟目录。 I have to do the following (code) to get to any virtual directories in nested folders:
我必须执行以下操作(代码)才能访问嵌套文件夹中的任何虚拟目录:
foreach (var directoryEntry in entry.Children.Cast<DirectoryEntry>().Where(directoryEntry => directoryEntry.SchemaClassName == "IIsWebVirtualDir" || directoryEntry.SchemaClassName == "IIsWebDirectory")) {
foreach (DirectoryEntry child in directoryEntry.Children.Cast<DirectoryEntry>().Where(subChild => subChild.SchemaClassName == "IIsWebVirtualDir")) {
if (child.Name == vDir)
return true;
}
if (directoryEntry.Name != vDir) continue;
return true;
}
Which to me is quite ugly. 对我来说,这很丑陋。 Is there a better way that I can check for existing virtual directories if they exist in sub folders?
有没有更好的方法可以检查子文件夹中是否存在现有的虚拟目录?
Are you looking for a recursive function to iterate the complete site? 您是否正在寻找递归函数来迭代整个站点?
public static void Main() {
var siteRoot = new DirectoryEntry("IIS://<servername>/W3SVC/2/Root");
var containsVirtualDirectory = ContainsVirtualDirectory(siteRoot);
}
private static Boolean ContainsVirtualDirectory(DirectoryEntry container) {
foreach (DirectoryEntry child in container.Children) {
if (child.SchemaClassName == "IIsWebVirtualDir")
return true;
if (child.SchemaClassName == "IIsWebDirectory" && ContainsVirtualDirectory(child))
return true;
}
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.