如何从Microsoft.SharePoint.Client.File对象获取文件大小?我正在寻找一种从Microsoft.SharePoint.Client.File对象获取文件大小的好方法。Client对象没有Length成员。我试过这个:foreach(SP.Filefileinfiles){stringpath=file.Path;path=path.Substring(this.getTeamSiteUrl().Length);FileInformationfileInformation=SP.File.OpenBinaryDirect(this.Context,path);使用(MemoryStreammemoryStream=newMemoryStream()){CopyStream(fileInformation.Stream,memoryStream);file.Size=memoryStream.Length;通过使用MemoryStream给我一个长度,但这对MemoryStream的性能不利。此文件也不属于文档库。由于它是一个附件,我无法使用ListItemAllFields将它转换为ListItem对象。如果我可以将它转换为ListItem,我可以使用它来获取它的大小:ListItem["File_x0020_Size"]如何使用C#在SharePoint中获取客户端对象的文件大小?加载File_x0020_Size字段信息获取。当我想列出Sharepoint2010文件夹中的所有文件时,我是这样做的:_ctx.Load(spFolder);_ctx.ExecuteQuery();FileCollectionfileCol=spFolder.Files;_ctx.Load(fileCol);_ctx.ExecuteQuery();foreach(Microsoft.SharePoint.Client.FilespFileinfileCol){//在这里,指定要检索的所有字段,包括文件大小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();intfileSize=int.Parse((string)spFile.ListItemAllFields["File_x0020_Size"]);}_ctx显然是你启动的ClientContext。这是所有Sharepoint内部字段的扩展列表不能只使用Stream属性的长度吗?file.Size=fileInformation.Stream.Length;我不知道这个问题是否解决了,但对于那些正在寻找答案的人(比如我):...codetogetSP.FILE...SP.FileInformationfileInfo=SP.File.OpenBinaryDirect(ctx,mySPFile.ServerRelativeUrl);byte[]bodyString=ReadToEnd(fileInfo.Stream);intlength=bodyString.Length;Console.Write(length.ToString());...做任何你需要的事...publicstaticbyte[]ReadToEnd(System.IO.Streamstream){longoriginalPosition=0;如果(stream.CanSeek){originalPosition=stream.Position;stream.Position=0;}try{byte[]readBuffer=newbyte[4096];inttotalBytesRead=0;intbytesRead;while((bytesRead=stream.Read(readBuffer,totalBytesRead,readBuffer.Length-totalBytesRead))>0){totalBytesRead+=bytesRead;如果(totalBytesRead==readBuffer.Length){intnextByte=stream.ReadByte();if(nextByte!=-1){byte[]temp=newbyte[readBuffer.Length*2];Buffer.BlockCopy(readB缓冲区,0,临时,0,readBuffer.Length);Buffer.SetByte(temp,totalBytesRead,(byte)nextByte);读取缓冲区=温度;totalBytesRead++;}}}byte[]buffer=readBuffer;if(readBuffer.Length!=totalBytesRead){buffer=newbyte[totalBytesRead];Buffer.BlockCopy(readBuffer,0,buffer,0,totalBytesRead);}返回缓冲区;}finally{if(stream.CanSeek){stream.Position=originalPosition;希望这对其他人有帮助,因为我在互联网上找不到直接的答案!以上就是C#学习教程:如何从Microsoft.SharePoint.Client.File对象获取文件大小?分享的所有内容,如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
