因此,我浏览了这篇文章以列出虚拟目录。这篇文章展示了如何为 DirectoryEntry 指定用户名和密码。但我仍然收到拒绝访问。
关于权限,我仍然没有掌握一些东西。有任何想法吗?
class Program
{
static void Main(string[] args)
{
//http://www.developmentnow.com/blog/2004/12/29/recursively-list-virtual-directories-in-iis-with-c-and-directoryservices/
// call the function once to kick it off
WriteVDirs("localhost", 2, "");
}
// function that recursively walks through IIS dir & vdirs & lists all the virtual directories
public static void WriteVDirs(string serverName, int SiteNumber, string path)
{
DirectoryEntry de =
new DirectoryEntry("IIS://" + serverName + "/W3SVC/" +
SiteNumber.ToString() + "/Root" + path);
de.Username = "my-machine/administrator";
de.Password = "admin";
DirectoryEntries dirs;
try
{
dirs = de.Children;
foreach (DirectoryEntry d in dirs)
{
if (0 == String.Compare(d.SchemaClassName, "IIsWebDirectory"))
{
string fullPath = path + "/" + d.Name;
WriteVDirs(serverName, SiteNumber, fullPath);
}
else if (0 == String.Compare(d.SchemaClassName, "IIsWebVirtualDir"))
{
string fullPath = path + "/" + d.Name;
Console.WriteLine(fullPath + " : " + d.Properties["Path"].Value);
WriteVDirs(serverName, SiteNumber, fullPath);
}
}
}
catch (Exception ex)
{
Console.WriteLine("ERROR: " + ex.Message);
}
}
}