遍历Web,下载其中的所有文件夹、列表、文档及其子Web
本文关键字:Web 列表 文档 文件夹 下载 遍历 | 更新日期: 2023-09-27 18:18:53
我正在尝试下载一个网站及其子网站和子网站(如果存在)等的所有内容(文档,列表,文件夹),我可以为单个网站做到这一点,但它不适用于其子网站,代码如下,
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();
}
}
}
}
}
}
}
这是因为您希望对子网站进行递归处理
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();
}
}
}
}
}
}
}