以编程方式将一个文档库中的文件夹/文件复制/移动到另一个文档库

时间:2016-03-02 14:20:30

标签: sharepoint directory

我有一个如下的图书馆:

List
---------Folder 1
---------File 0
---------File 1
-----------------Folder 2
------------------------File 2
------------------------File 3
---------Folder 3
-----------------File 4
-----------------File 5

计划是将它复制到另一个库,我找到了一种方法来复制文件0 /文件1.但我正在寻找一种方法将所有库(带有子文件夹和文件的文件夹)复制到我的其他图书馆。

要复制文件,我这样做了:

SPListItem item = oldLib.GetItemById(collItem[i].ID);
byte[] fileBytes = item.File.OpenBinary();
string destUrl = newlib.RootFolder.Url + "/" + item.File.Name;
SPFile destFile = newlib.RootFolder.Files.Add(destUrl, fileBytes, true);

我是否想念一个简单的方法来做我想要的事情?

祝你好运

1 个答案:

答案 0 :(得分:0)

SPList sourceLibrary = web.Lists["SourceLibraryName"];

SPFolderCollection subFolders = sourceLibrary.RootFolder.SubFolders;

foreach (SPFolder subFolder in subFolders)

{

if (subFolder.SubFolders.Count == 0)

{

Console.WriteLine(subFolder.Name + " have no subfolders inside");

//**Proceed with copying files

}

else

{

Console.WriteLine("*******"+subFolder.Name + " have subfolders inside*******");

foreach (SPFolder subSubFolder in subFolder.SubFolders)

{

Console.WriteLine(subFolder.Name);

//**Do the same as above

}

}

}

Console.ReadKey();