我想創建一個IIS站點內的所有虛擬目錄的列表。但是,我發現在老版本的IIS中嘗試這樣做會有很大不同。在IIS 7,這是通過C#相對輕鬆的工作,但我似乎無法找到一個很好的方法,在IIS 6中這樣做的,5列出IIS 5,6和7中的所有虛擬目錄
我曾嘗試使用System.DirectoryServices.DirectoryEntry但不似乎給了我想要的輸出。
我是服務器管理員,所以我願意使用其他內容,比如內置於IIS中的.vbs文件以及編寫自己的代碼。
我想創建一個IIS站點內的所有虛擬目錄的列表。但是,我發現在老版本的IIS中嘗試這樣做會有很大不同。在IIS 7,這是通過C#相對輕鬆的工作,但我似乎無法找到一個很好的方法,在IIS 6中這樣做的,5列出IIS 5,6和7中的所有虛擬目錄
我曾嘗試使用System.DirectoryServices.DirectoryEntry但不似乎給了我想要的輸出。
我是服務器管理員,所以我願意使用其他內容,比如內置於IIS中的.vbs文件以及編寫自己的代碼。
你嘗試過在將GetObject( 「IIS://服務器/ W3SVC」)
你做到這一點在VBS這樣
'Get the IIS Server Object
Set oW3SVC = GetObject("IIS://ServerName/W3SVC/1/ROOT")
For Each oVirtualDirectory In oW3SVC
Set oFile = CreateObject("Scripting.FileSystemObject")
Set oTextFile = oFile.OpenTextFile("C:\Results.txt", 8, True)
oTextFile.WriteLine(oVirtualDirectory.class + " -" + oVirtualDirectory.Name)
oTextFile.Close
Next
Set oTextFile = Nothing
Set oFile = Nothing
Wscript.Echo "Done"
我有一個關於它的文章在這裏 - >http://anyrest.wordpress.com/2010/02/10/how-to-list-all-websites-in-iis/
以下是兩個應該跨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);
}
}
我知道你問VB解決方案成功通過測試兩個,我不知道真的很瞭解VB,我更喜歡C#人。總之,這裏是一個應用程序所採取的C#.NET代碼一點點我寫了前一段時間,它列出了IIS虛擬目錄...
using System.DirectoryServices;
private DirectoryEntry _iisServer = null;
private DirectoryEntry iisServer
{
get
{
if (_iisServer == null)
{
string path = string.Format("IIS://{0}/W3SVC/1", serverName);
_iisServer = new DirectoryEntry(path);
}
return _iisServer;
}
}
private IDictionary<string, DirectoryEntry> _virtualDirectories = null;
private IDictionary<string, DirectoryEntry> virtualDirectories
{
get
{
if (_virtualDirectories == null)
{
_virtualDirectories = new Dictionary<string, DirectoryEntry>();
DirectoryEntry folderRoot = iisServer.Children.Find("Root", VirDirSchemaName);
foreach (DirectoryEntry virtualDirectory in folderRoot.Children)
{
_virtualDirectories.Add(virtualDirectory.Name, virtualDirectory);
}
}
return _virtualDirectories;
}
}
希望你將能夠從得到的總體思路。
這裏有兩個輔助函數添加到Garetts的答案。有了這些,您可以遍歷每個域並獲取其所有虛擬目錄,包括不在域根文件夾中的虛擬目錄。
從域名獲取網站ID:
string GetSiteID(string domain)
{
string siteId = string.Empty;
DirectoryEntry iis = new DirectoryEntry("IIS://localhost/W3SVC");
foreach (DirectoryEntry entry in iis.Children)
if (entry.SchemaClassName.ToLower() == "iiswebserver")
if (entry.Properties["ServerComment"].Value.ToString().ToLower() == domain.ToLower())
siteId = entry.Name;
if (string.IsNullOrEmpty(siteId))
throw new Exception("Could not find site '" + domain + "'");
return siteId;
}
獲取站點條目所有子項(遞歸)
static DirectoryEntry[] GetAllChildren(DirectoryEntry entry)
{
List<DirectoryEntry> children = new List<DirectoryEntry>();
foreach (DirectoryEntry child in entry.Children)
{
children.Add(child);
children.AddRange(GetAllChildren(child));
}
return children.ToArray();
}
地圖網站ID對大量網站
的編輯:船尾呃用包含數百個域的服務器測試這個,GetSiteID()執行速度非常慢,因爲它一次又一次地枚舉所有站點以獲取該id。下面的函數只枚舉這些站點一次,並將每個站點映射到它的id並將其存儲在字典中。然後,當您需要站點ID時,您可以從詞典中將其拉出,而站點[「domain」]。如果您正在爲服務器上的所有站點查找虛擬目錄,或者大量查找虛擬目錄,則下面的操作將比上面的GetSiteID()快得多。
public static Dictionary<string, string> MapSiteIDs()
{
DirectoryEntry IIS = new DirectoryEntry("IIS://localhost/W3SVC");
Dictionary<string, string> dictionary = new Dictionary<string, string>(); // key=domain, value=siteId
foreach (DirectoryEntry entry in IIS.Children)
{
if (entry.SchemaClassName.ToLower() == "iiswebserver")
{
string domainName = entry.Properties["ServerComment"].Value.ToString().ToLower();
string siteID = entry.Name;
dictionary.Add(domainName, siteID);
}
}
return dictionary;
}
酷!節省我的時間:) – Garett 2012-05-09 23:44:40
我想要做類似的事情,但是從DOS:http://stackoverflow.com/q/13218046/589059 – rkagerer 2012-11-04 11:22:47