使用 ClientContext 在 SharePoint(在线)文件传输期间保留修改日期

Retain Modified Date During SharePoint (online) File Transfers Using ClientContext

如果 SharePoint 有较新的文件副本,我实际上是在尝试从 SharePoint 下载,如果我的文件较新,则推送我的。我从不关心合并此应用程序中的更改。越新越好,因为数据都是来自服务器的原始查询。也就是说,这就是我正在做的比较哪个文件较新并相应地上传或下载的操作。问题是,每次上传和下载都会使“修改日期”等于我进行传输的确切时间。我发现我可以从 SharePoint 获取修改日期并覆盖 Windows 时间戳,但我不确定如何采用其他方式。如果有一种方法可以根据对我当前代码的影响来保留开始的值,我也会接受。那么我该如何保留或修改它呢?

        using (var ctx = new ClientContext(DataSharepointSite))
        {
            ctx.AuthenticationMode = ClientAuthenticationMode.Default;
            ctx.Credentials = GetSharepointCredentials();

            var file = ctx.Web.GetFileByServerRelativeUrl(sharepointFile);
            var fileData = file.OpenBinaryStream();
            ctx.Load(file);
            ctx.ExecuteQuery();
            using (var sr = new StreamReader(fileData.Value))
            {
                DateTime lastDataUpdate = System.IO.File.GetLastWriteTime(localFile).ToUniversalTime();
                DateTime ShareDate = file.TimeLastModified.ToUniversalTime();

                // If SharePoint has a newer file, download it
                if (ShareDate > lastDataUpdate)
                    using (FileStream fs = new FileStream(localFile, FileMode.Create))
                    {
                        sr.BaseStream.CopyTo(fs);
                    }
                    // Forced to overwrite modified date to match SharePoint
                    System.IO.File.SetLastWriteTime(localFile, ShareDate);
                }
                // else if local copy is newer, push to SharePoint
                else if (ShareDate < lastDataUpdate)
                {
                    using (FileStream fs = new FileStream(localFile, FileMode.Open))
                    {
                        // This, too, is getting a new "modified date" so next compare will re-download
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, sharepointFile, fs, true);
                    }
                }
            }
file.ListItemAllFields["Modified"] = System.IO.File.GetLastWriteTime(localFile);
file.ListItemAllFields.Update();
ctx.ExecuteQuery();