簡體   English   中英

如何使用WMI閱讀IIS 6網站的目錄結構?

[英]How to read an IIS 6 Website's Directory Structure using WMI?

我需要在IIS 6.0中使用WMI和C# 讀取網站的文件夾 我能夠使用“IISWebVirtualDirSetting”類讀取虛擬目錄和應用程序。 但是, 使用此類無法讀取位於網站內物理文件夾。

對於我的情況,我需要閱讀位於網站內的子文件夾,然后再設置它們的權限。 根據我的要求,我不需要處理虛擬目錄/ Web服務應用程序(可以使用下面的代碼輕松獲取..)。

我曾嘗試使用IISWebDirectory類,但它很有用。

以下是讀取IIS虛擬目錄的代碼...

public static ArrayList RetrieveVirtualDirList(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    String SiteId = GetSiteIDFromSiteName(ServerName, WebsiteName);
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IISWebVirtualDirSetting");
    //ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        String temp = WebsiteName.Substring(0, WebsiteName.IndexOf('/') + 1);
        String VirtualDirName = WebsiteName.Substring(temp.Length);
        WebsiteName = WebsiteName.Replace(SiteId, "");
        if (extrctedSiteId.Equals(SiteId))
        //if (true)
        {
            WebSiteListArray.Add(VirtualDirName );
            //WebSiteListArray.Add(WebSiteName);
            //+ "|" + WebSite.Properties["Path"].Value.ToString()
        }
    }
    return WebSiteListArray;
}

PS:

我需要以編程方式在ASP中使用WMI和C#獲取已部署站點的子文件夾。 凈申請。 我需要找到本地或遠程IIS 6.0 Web服務器中現有網站的子文件夾。 所以我需要一個程序化的解決方案。 確切地說,如果我指向正確的類(如IISWebVirtualDirSetting等),我可以用它來檢索網站中的物理文件夾列表,那么它將非常有用。

我不是在Powershell工作,我真的不需要涉及PowerShell或vbscripts的解決方案。

在C#/ ASP.Net中做同樣的任何其他編程方式也將受到高度贊賞。

編輯:

GetSiteIdFromSiteName

代碼可能如下所示:

public static int GetSiteIdFromSiteName(String ServerName, String WebsiteName)
{
    ConnectionOptions options = SetUpAuthorization();
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\MicrosoftIISV2", ServerName), options);
    scope.Connect();
    ObjectQuery OQuery = new ObjectQuery(@"SELECT * FROM IIsSetting");

    ManagementObjectSearcher WebSiteFinder = new ManagementObjectSearcher(scope, OQuery);
    ArrayList WebSiteListArray = new ArrayList();
    ManagementObjectCollection WebSitesCollection = WebSiteFinder.Get();
    String WebSiteName = String.Empty;
    foreach (ManagementObject WebSite in WebSitesCollection)
    {
        WebSiteName = WebSite.Properties["Name"].Value.ToString();
        WebsiteName = WebSiteName.Replace("W3SVC/", "");
        String extrctedSiteId = WebsiteName.Substring(0, WebsiteName.IndexOf('/'));
        break;  
    }
    return extrctedSiteId;
}

SetupAuthorization()

這是SetupAuthorization函數。

    public ConnectionOptions SetUpAuthorization()
{ 
        ConnectionOptions options = new ConnectionOptions();
    //options.Authentication = AuthenticationLevel.Connect; 
    //may need to set Packetprivacy enum
    options.Authentication = AuthenticationLevel.PacketPrivacy; 
    options.EnablePrivileges = true; 
    options.Impersonation = ImpersonationLevel.Impersonate; 

     return options;
 } 

問候

沒關系。 我自己搞定了。 如果目錄結構在本地系統上,則需要System.IO.DirectoryInfo。 使用遠程處理也可以用於遠程服務器。

參考: http //msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx

如果WMi必須在那里,那么Win32_Directory是應該在查詢中使用的類:

"Select * from Win32_directory where drive ='"+ DriveLetter +":' and Path ='%" + MyPath + "%'";

參考: http //msdn.microsoft.com/en-us/library/aa394130(VS.85).aspx

你看過Web部署工具了嗎?

http://www.iis.net/expand/WebDeploy

能夠打包ACL,COM,GAC和注冊表設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM