0

我正在尝试下载网络及其子网络及其子网络(如果存在)的所有内容(文档、列表、文件夹)等等,我可以为单个网络执行此操作,但它不适用于其中的子网络,代码如下,

   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();
                                }
                            }
                        }
                }
            }
        }
    }
4

1 回答 1

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();
                                }
                            }
                        }
                }
            }
        }
}
于 2011-12-28T17:37:24.747 回答