vb.net 使用 CSOM 打开和读取位于 SharePoint 文档库中的文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33783742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open and Read a text file Located in a SharePoint Document Library using CSOM
提问by FlyFish
I'm able to get the relative path to a file in SharePoint. I now need to open the file and read its contents. This is where I'm stuck. I don't know how to open a file for reading unless it is local to my hard drive, which it is not. Here is my code:
我能够在 SharePoint 中获取文件的相对路径。我现在需要打开文件并阅读其内容。这就是我被困的地方。我不知道如何打开文件进行读取,除非它在我的硬盘驱动器本地,但事实并非如此。这是我的代码:
If item.FieldValues("File_x0020_Type") = "html" Then
Dim the_file As SP.File = oWebsite.GetFileByServerRelativeUrl(item.FieldValues("FileRef"))
Dim reader As StreamReader = New StreamReader(the_file)
Dim sr As StreamReader = StreamReader(the_file)
textbox1.text = sr.ReadToEnd()
reader.Close()
End If
回答by Nefariis
Sorry misread that.
抱歉误读了。
ClientContext clientContext = new ClientContext("http://SpSiteUrl");
clientContext.ExecuteQuery();
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, "thisFile");
System.IO.Stream stream = fileInfo.Stream;
using (StreamReader r = new StreamReader(stream))
string line;
while((line = file.ReadLine()) != null)
{
System.Console.WriteLine (line);
}
}
Once it is set to the Stream, you should be able to loop through it normally.
一旦它被设置为 Stream,你应该能够正常循环它。
I also saw this second method.
我也看到了第二种方法。
using (var clientContext = new ClientContext(url)) {
Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, "thisFile");
using (var fileStream = System.IO.File.Create(getItem))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
Then you would just loop through the fileStream normally as well.
然后你也可以正常循环遍历 fileStream 。
Here is a second way to loop as well -
这也是第二种循环方式 -
using (StreamReader sr = new StreamReader(stream))
{
while (sr.Peek() >= 0)
{
Console.WriteLine(sr.ReadLine());
}
}
And actually, now that I am reading your question one more time - you might be able to do just this.
事实上,现在我又读了一遍你的问题——你也许可以做到这一点。
Dim reader As StreamReader = New StreamReader(stream)
textbox1.text = reader.ReadToEnd()
reader.Close()

