2011-12-28 38 views
0

我試圖下載網絡及其子網和其子網的所有內容(文檔,列表,文件夾)(如果存在)等等,我可以做一個單一的網絡,但它不是工作在它的子網站,代碼如下,迭代通過Web下載其中的所有文件夾,列表,文檔及其子網絡

private void downloadList(SPObjectData objectData) 
    { 
     using (SPWeb currentWeb = objectData.Web) 
     { 
      foreach (SPList list in currentWeb.Lists) 
      { 
        foreach (SPFolder oFolder in list.Folders) 
        { 
         if (oFolder != null) 
         { 
          foreach (SPFile file in oFolder.files) 
          { 
           if (CreateDirectoryStructure(tbDirectory.Text, file.Url)) 
           { 
            var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url); 
            byte[] binFile = file.OpenBinary(); 
            System.IO.FileStream fstream = System.IO.File.Create(filepath); 
            fstream.Write(binFile, 0, binFile.Length); 
            fstream.Close(); 
           } 
          } 
         } 
       } 
      } 
     } 
    } 
+0

乾杯,我不這麼認爲。什麼不起作用?你有例外嗎?它感覺你想讓我們調試你的代碼... – rene 2011-12-28 17:04:57

+0

我認爲你沒有;不正確地閱讀我的問題:),無論如何我再解釋一次,問題是我可以得到它爲1單個網絡工作,但我努力尋找一種下載整個網站的方法,或者一個帶有子網站的網站等等。乾杯 – 2011-12-28 17:31:31

回答

0

那是因爲你要爲子網站做遞歸

foreach(SPWeb oWeb in currentWeb.Webs){ 

downloadList(oWeb); //use same logic you used above to get all the stuff from the sub web 

} 

所以,它會是這樣的你的遞歸方法:

//notice I overloaded 
private void downloadList(SPWeb oWeb){ 
//get subwebs of subwebs 
foreach(SPWeb oWeb in currentWeb.Webs){ 

    downloadList(oWeb); 

} 
foreach (SPList list in oWeb.Lists) 
      { 
        foreach (SPFolder oFolder in list.Folders) 
        { 
         if (oFolder != null) 
         { 
          foreach (SPFile file in oFolder.files) 
          { 
           if (CreateDirectoryStructure(tbDirectory.Text, file.Url)) 
           { 
            var filepath = System.IO.Path.Combine(tbDirectory.Text, file.Url); 
            byte[] binFile = file.OpenBinary(); 
            System.IO.FileStream fstream = System.IO.File.Create(filepath); 
            fstream.Write(binFile, 0, binFile.Length); 
            fstream.Close(); 
           } 
          } 
         } 
       } 
      } 
     } 
} 
+0

你的代碼有些混亂,請查看它:)當前Web不存在的上下文加上我使用SPObjectData不SPWeb作爲參數,如果我通過SPWeb作爲參數將有內存管理的問題,它不會妥善處理它。乾杯 – 2011-12-29 08:54:22

相關問題