C# 下载SharePoint文件夹中的最新文件

C# 下载SharePoint文件夹中的最新文件,c#,filestream,httpwebresponse,C#,Filestream,Httpwebresponse,我的当前代码从SharePoint文件的绝对URL下载该文件并写入本地 我想将它改为使用文件夹URL,并根据一些过滤器下载文件夹中的文件 能做到吗 下面是我当前的代码片段: string fullFilePath = DownloadSPFile("http://MySharePointSite.com/sites/Collection1/Folder1/File1.docx/"); public static string DownloadSPFile(string urlPath) {

我的当前代码从SharePoint文件的绝对URL下载该文件并写入本地

我想将它改为使用文件夹URL,并根据一些过滤器下载文件夹中的文件

能做到吗

下面是我当前的代码片段:

string fullFilePath = DownloadSPFile("http://MySharePointSite.com/sites/Collection1/Folder1/File1.docx/");

public static string DownloadSPFile(string urlPath)
{
    string serverTempdocPath = "";

    try
    {
        var request = (HttpWebRequest)WebRequest.Create(urlPath);
        var credentials = new NetworkCredential("username", "password", "domain");

        request.Credentials = credentials;
        request.Timeout = 20000;
        request.AllowWriteStreamBuffering = false;

        using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
        {
            using (Stream stream = response.GetResponseStream())
            {
                serverTempdocPath = Path.Combine(AppConfig.EmailSaveFilePath + "_DOWNLOADED.eml");

                using (FileStream fs = new FileStream(serverTempdocPath, FileMode.Create))
                {
                    byte[] read = new byte[256];
                    int count = stream.Read(read, 0, read.Length);
                    while (count > 0)
                    {
                        fs.Write(read, 0, count);
                        count = stream.Read(read, 0, read.Length);
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        AppLogger.LogError(ex, "");
        throw ex;
    }
    return serverTempDocPath;
}

您是否尝试设置urlPath的格式

比如:
string urlPath=“path/to/folder/”;
字符串fileFilter=“Cat.png”;
string finalPath=string.format(“{0}{1}”,urlPath,fileFilter)
(或类似的事情)


(希望我理解您的问题,并且能够帮助您^^)

如果您的sharepoint网站启用sharepoint rest api,您可以非常轻松地获得详细信息

获取文件列表

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"
并传递查询

"?filter=$top=1&$orderby=Created desc"

不记得确切的api,但是有一个api,给定文件夹路径,您可以列出文件夹中的文件。然后形成您感兴趣的单个文件URL。依稀记得是SharePoint列表服务。@Pratekshrivastava我会在谷歌上搜索,看看是否能找到它!谢谢,你明白我的意思了。由于文件名未知,我需要获取文件夹中的
最新文件。所以这有点头疼。呃,有点棘手。感谢,您可能可以获得时间信息,但我认为您必须自己进行筛选,并对文件夹中的所有现有文件进行迭代:/但是如果您使用SharePoint REST服务,您可能可以找到预先存在的筛选,以帮助您下载最新的文件。请查看网站。他们讨论根据修改时间列出文档。希望它能帮助你:)