码迷,mamicode.com
首页 > 其他好文 > 详细

文件深层递归

时间:2015-01-11 12:12:19      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

public SPFolderModel GetDocumentInfo(string folderPath)
{
var rootFolder = new SPFolderModel();
rootFolder = GetFileAndFolderInfo(folderPath);
foreach (var folder in rootFolder.SubFolders)
{
var subfolder = GetFileAndFolderInfo(folderPath + "\\" + folder.Name);
folder.SubFolders = subfolder.SubFolders;
folder.Files = subfolder.Files;
}
return rootFolder;
}

public SPFolderModel GetFileAndFolderInfo(string folderPath)
{
var folderModel = new SPFolderModel();
Folder docLib = _website.GetFolderByServerRelativeUrl(folderPath);
_clientContext.Load(docLib);
_clientContext.Load(docLib.Files);
_clientContext.Load(docLib.Folders);
var folders = new List<SPFolderModel>();
try
{
_clientContext.ExecuteQuery();
folderModel.Files = GetFiles(folderPath,docLib);
foreach (var folder in docLib.Folders)
{
folders.Add(new SPFolderModel()
{
Name = folder.Name,
FolderPath = folderPath
});
}
folderModel.SubFolders = folders;
}
catch (Exception ex)
{
throw ex;
}
return folderModel;
}
private List<SPFileModel> GetFiles(string folderPath,Folder docLib)
{
var resFiles = new List<SPFileModel>();
if (docLib.Files.Count > 0)
{
foreach (var file in docLib.Files)
{
var url = Config.TargetFileUrl + folderPath+"\\"+file.Name;
//var id = file.ContentTag.Substring(0, file.ContentTag.IndexOf(","));
//url = string.Format(url, id, file.Name);
resFiles.Add(new SPFileModel()
{
Name = file.Name,
Url = url,
FolderPath = folderPath,
LastModifiedTime = file.TimeLastModified,
Size = Convert.ToInt64((file.Length) / 1024)
});
}
}
return resFiles;
}

------------------------------------------------------------------------------------------------

public List<SPFileModel> GetAllFiles(SPFolderModel spFolder)
{
var resFiles = new List<SPFileModel>();
GetFilesByFolderDeep(ref resFiles, spFolder);
return resFiles;
}

public List<SPFileModel> GetFilesByFolderDeep(ref List<SPFileModel> resFiles, SPFolderModel spFolder)
{
List<SPFileModel> files = new List<SPFileModel>();
if (spFolder.Files.Count > 0)
{
resFiles.AddRange(spFolder.Files);
}
if (spFolder.SubFolders.Count > 0)
{
foreach (var subFolder in spFolder.SubFolders)
{
var subFolderFiles = new List<SPFileModel>();
GetFilesByFolderDeep(ref subFolderFiles, subFolder);
resFiles.AddRange(subFolderFiles);
}
}
return files;
}

文件深层递归

标签:

原文地址:http://www.cnblogs.com/csharphuang/p/4216217.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!