2009-10-19 137 views
2

如何從Web目錄獲取文件列表?如果我訪問網絡目錄URL,則Internet瀏覽器會列出該目錄中的所有文件。現在我只想在C#中獲取該列表並將它們下載到BITS(後臺智能傳輸服務)中。如何從Web目錄獲取文件列表?

回答

0

這是我最近調查過的一個有趣的話題。如你所知,你可以通過COM訪問位,但這裏有幾個項目,使其更容易:

SharpBITS.NET
Forms Designer Friendly Background Intelligent Transfer Service (BITS) wrapper

article on MSDN可能比你想知道多一點。

我嘗試了CodeProject鏈接中的代碼,它似乎工作得很好。 CodePlex項目看起來非常好,但我還沒有嘗試過。

+0

注意:我在我的項目中使用SharpBITS.net。我同意似乎非常好。 – Eric 2009-10-20 16:33:41

3

關於「獲得在C#中該列表」部分:

foreach (string filename in 
    Directory.GetFiles(
     Server.MapPath("/"), "*.jpg", 
     SearchOption.AllDirectories)) 
{ 
    Response.Write(
     String.Format("{0}<br />", 
      Server.HtmlEncode(filename))); 
} 
+0

感謝您的回覆。這是創建列表的好方法,但我不想創建我想要讀取並解析目錄中的默認IIS文件列表的列表。 – Eric 2009-10-20 20:51:12

+0

@Eric,對不起,我不明白你想要完成什麼;你能否詳細說明你的問題? – 2009-10-20 22:08:17

+0

我不明白,什麼是變量:服務器? – 2016-09-21 15:58:36

0

好吧,如果Web服務器允許列出文件有問題的目錄,你是好去。

不幸的是,Web服務器應該如何返回你的列表沒有標準。它通常以HTML格式,但HTML在多個Web服務器上的格式並不總是相同。

如果您想要始終從同一個Web服務器上的相同目錄下載文件,只需在Web瀏覽器中的目錄中執行「查看源代碼」即可。然後嘗試編寫一個小的正則表達式,它將抓取HTML源文件中的每個文件名。

然後,您可以創建一個Web客戶端,請求目錄URL,解析響應您的正則表達式來獲取文件名,然後再處理您的BITS客戶端的文件

希望這有助於

0
private void ListFiles() 
{ 

    //get the user calling this page 
    Gaf.Bl.User userObj = base.User; 
    //get he debug directory of this user 
    string strDebugDir = userObj.UserSettings.DebugDir; 
    //construct the Directory Info directory 
    DirectoryInfo di = new DirectoryInfo(strDebugDir); 
    if (di.Exists == true) 
    { 

     //get the array of files for this 
     FileInfo[] rgFiles = di.GetFiles("*.html"); 
     //create the list ... .it is easier to sort ... 
     List<FileInfo> listFileInfo = new List<FileInfo>(rgFiles); 
     //inline sort descending by file's full path 
     listFileInfo.Sort((x, y) => string.Compare(y.FullName, x.FullName)); 
     //now print the result 
     foreach (FileInfo fi in listFileInfo) 
     { 
      Response.Write("<br><a href=" + fi.Name + ">" + fi.Name + "</a>"); 
     } //eof foreach 
    } //eof if dir exists 

} //eof method 
相關問題