gpt4 book ai didi

c# - 列出 IIS 5,6 和 7 中的所有虚拟目录

转载 作者:太空狗 更新时间:2023-10-30 00:57:59 24 4
gpt4 key购买 nike

我正在尝试创建 IIS 站点内所有虚拟目录的列表。但是,我发现在旧版本的 IIS 中尝试执行此操作有很大差异。在 IIS 7 中,这是通过 C# 完成的一项相对容易的任务,但我似乎找不到在 IIS 6 和 5 中执行此操作的好方法。

我尝试过使用 System.DirectoryServices.DirectoryEntry,但这似乎没有给我所需的输出。

我是服务器管理员,所以我愿意使用其他东西,例如 IIS 中内置的 .vbs 文件以及编写我自己的代码。

最佳答案

这里有两个示例应该可以跨 IIS 5、6 和 7(安装了 IIS 6 WMI 兼容性)。我成功地测试了 IIS 5 和 7。

VBScript 版本

Function ListVirtualDirectories(serverName, siteId) 
Dim webSite
Dim webDirectory

On Error Resume Next

Set webSite = GetObject( "IIS://" & serverName & "/W3SVC/" & siteId & "/ROOT" )
If ( Err <> 0 ) Then
Err = 0
Exit Function
Else
For Each webDirectory in webSite
If webDirectory.Class = "IIsWebVirtualDir" Then
WScript.Echo "Found virtual directory " & webDirectory.Name
End If
Next
End If
End Function

C#版本

void ListVirtualDirectories(string serverName, int siteId)
{
DirectoryEntry webService = new DirectoryEntry("IIS://" + serverName + "/W3SVC/" + siteId + "/ROOT");

foreach (DirectoryEntry webDir in webService.Children)
{
if (webDir.SchemaClassName.Equals("IIsWebVirtualDir"))
Console.WriteLine("Found virtual directory {0}", webDir.Name);
}
}

关于c# - 列出 IIS 5,6 和 7 中的所有虚拟目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3938467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com