gpt4 book ai didi

c# - 使用 C#.net 从 Sharepoint Online 下载文件?

转载 作者:行者123 更新时间:2023-12-03 08:52:36 25 4
gpt4 key购买 nike

我想将文件从 SharePoint Online 下载到本地,但我正在努力处理代码示例。我在谷歌上搜索过,但没有得到任何有值(value)的信息。

最佳答案

尝试下面的代码片段,使用 SharePoint Online CSOM 将文件从库下载到本地:

using Microsoft.SharePoint.Client;
using System.IO;
using System.Linq;
using System.Security;

class Program
{
static void Main(string[] args)
{
using (ClientContext ctx = new ClientContext(
"https://tenantname.sharepoint.com/sites/sitename/"))
{
string password = "********";
string account = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="41343224332f202c240135242f202f352f202c246f2e2f2c2822332e322e27356f222e2c" rel="noreferrer noopener nofollow">[email protected]</a>";
var secret = new SecureString();
foreach (char c in password)
{
secret.AppendChar(c);
}
ctx.Credentials = new SharePointOnlineCredentials(account, secret);
ctx.Load(ctx.Web);
ctx.ExecuteQuery();

List list = ctx.Web.Lists.GetByTitle("libraryTitle");

FileCollection files = list.RootFolder.Folders
.GetByUrl("/sites/sitename/shared documents/foldername").Files;

ctx.Load(files);
ctx.ExecuteQuery();

foreach (Microsoft.SharePoint.Client.File file in files)
{
FileInformation fileinfo = Microsoft.SharePoint.Client.File
.OpenBinaryDirect(ctx, file.ServerRelativeUrl);

ctx.ExecuteQuery();

using (FileStream filestream =
new FileStream("C:" + "\\" + file.Name, FileMode.Create))
{
fileinfo.Stream.CopyTo(filestream);
}
}
};
}
}

关于c# - 使用 C#.net 从 Sharepoint Online 下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58034425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com