c# - 如何从 Microsoft.SharePoint.Client.File 对象获取文件大小?

标签 c# .net sharepoint sharepoint-2010

我正在寻找一种从 Microsoft.SharePoint.Client.File 对象获取文件大小的好方法。

Client 对象没有 Length 成员。

我试过这个:

foreach (SP.File file in files)
{
    string path = file.Path;
    path = path.Substring(this.getTeamSiteUrl().Length);
    FileInformation fileInformation = SP.File.OpenBinaryDirect(this.Context, path);
    using (MemoryStream memoryStream = new MemoryStream())
    {
        CopyStream(fileInformation.Stream, memoryStream);
        file.Size = memoryStream.Length;
    }
}

这通过使用 MemoryStream 给了我一个长度,但它对性能不利。此文件也不属于文档库。由于它是一个附件,我无法使用 ListItemAllFields 将它转换为 ListItem 对象。如果我可以将它转换为 ListItem,我可以使用以下方法获取它的大小:ListItem["File_x0020_Size"]

如何使用 C# 在 SharePoint 中获取 Client 对象的文件大小?

最佳答案

加载File_x0020_Size字段信息获取。

当我想列出 Sharepoint 2010 文件夹中的所有文件时,我会这样做:

//folderPath is something like /yoursite/yourlist/yourfolder
Microsoft.SharePoint.Client.Folder spFolder = _ctx.Web.GetFolderByServerRelativeUrl(folderPath);

_ctx.Load(spFolder);
_ctx.ExecuteQuery();

FileCollection fileCol = spFolder.Files;
_ctx.Load(fileCol);
_ctx.ExecuteQuery();

foreach (Microsoft.SharePoint.Client.File spFile in fileCol)
{
    //In here, specify all the fields you want retrieved, including the file size one...
    _ctx.Load(spFile, file => file.Author, file => file.TimeLastModified, file=>file.TimeCreated, 
                            file => file.Name, file => file.ServerRelativeUrl, file => file.ListItemAllFields["File_x0020_Size"]);
    _ctx.ExecuteQuery();

    int fileSize = int.Parse((string)spFile.ListItemAllFields["File_x0020_Size"]);
}

_ctx 显然是你发起的ClientContext

这是一个 extended list所有 Sharepoint 内部字段的

关于c# - 如何从 Microsoft.SharePoint.Client.File 对象获取文件大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8339483/

相关文章:

c# - 从 OneNote 中保存嵌入对象

c# - 使共享库可用于多个应用程序的最佳方法是什么?

c# - 在 .NET 中对类型参数进行约束有什么意义(基类和接口(interface)约束)

sharepoint - 如何从共享点列表项链接到文档库中的文件夹?

c# - SharePoint ListItem 错误 : "Value does not fall within the expected range"

c# - .NET 的规则引擎

c# 控制台应用程序 - 在不干扰其余执行代码的情况下运行 for(;;) 循环

.net - 报表查看器 Web 控件需要 Web 窗体上的 System.Web.UI.ScriptManager

.net - 访问 Azure 存储帐户中的所有队列

c# - 无法使用 REST 在 SharePoint 2013 中重命名文件夹