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

use*_*964 5 .net c# 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;
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

Fra*_*rme 5

加载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"]);
}
Run Code Online (Sandbox Code Playgroud)

_ctx显然是ClientContext您发起的。

这是所有Sharepoint内部字段的扩展列表


Bri*_*haw 0

不能只使用 Stream 属性的长度吗?

file.Size = fileInformation.Stream.Length;
Run Code Online (Sandbox Code Playgroud)