c# - 如何将 Onedrive<Item> 的内容写入本地文件

标签 c# uwp win-universal-app windows-10-universal onedrive

在我的应用中,我使用 OneDrive 来保持数据同步。我已成功将文件写入 OneDrive,但无法用较新的 OneDrive 数据替换本地过时数据。

我当前的方法在不抛出异常的情况下完成,不会返回 OneDrive 上的文件包含的相同文本数据。 该方法的目标是将OneDrive文件的修改日期与本地文件进行比较,如果OneDrive较新,则将OndeDrive文件的内容写入本地StorageFile,然后返回进行反序列化。

private async Task<string> GetSavedDataFileAsync(string filename)
    {
        string filepath = _appFolder + @"\" + KOWGame + @"\" + filename;
        StorageFile localread;
        BasicProperties localprops = null;
        string txt;
        try
        {
            localread = await local.GetFileAsync(filepath);
            localprops = await localread.GetBasicPropertiesAsync();
        }
        catch (FileNotFoundException)
        { localread = null; }
        if (_userDrive != null)
        {
            if (_userDrive.IsAuthenticated)
            {
                try
                {
                    Item item = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();
                    if (item != null)
                    {
                        DateTimeOffset drivemodified = (DateTimeOffset)item.FileSystemInfo.LastModifiedDateTime;
                        if (localprops != null)
                        {
                            if (drivemodified > localprops.DateModified)
                            {
                                Stream stream = await localread.OpenStreamForWriteAsync();
                                using (stream)
                                { await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync(); }
                            }
                        }
                    }
                }
                catch (OneDriveException e)
                {
                    if (e.IsMatch(OneDriveErrorCode.ActivityLimitReached.ToString()))
                    { string stop; }
                }
            }
        }
        if (localread == null) return string.Empty;
        txt = await FileIO.ReadTextAsync(localread);
        return txt;
    }

我试图对我在 Stack 上找到的另一个关于将 StorageFile 写入 OneDrive 的答案进行逆向工程,因为我需要打开本地文件的流,但我似乎没有正常工作。

最佳答案

要获取 OneDrive 项目的内容,我们需要使用以下方法:

var contentStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync();

使用时

await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();

您得到的是 OneDrive Item 而不是它的内容。

因此您可以像下面这样更改代码,将 Onedrive 项目的内容写入本地文件:

if (drivemodified > localprops.DateModified)
{
    using (var stream = await localread.OpenStreamForWriteAsync())
    {
        using (var contentStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync())
        {
            contentStream.CopyTo(stream);
        }
    }
}

关于c# - 如何将 Onedrive<Item> 的内容写入本地文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37095185/

相关文章:

C# 事件处理程序问题

c# - 起订量 : FileNotFoundException: Could not load file or assembly 'System.Runtime'

c# - UWP 中基于类的条件 xaml 布局

c++ - Windows 8/10 中事件窗口的进程名称

c# - UWP (C#) - 查看 LocalFolder 中的文件

c# - MobileServiceClient.LoginAsync() 引发方法 'LoginAsync' 没有重载需要 1 个参数错误

c# - 使用 $.ajax 同时运行多个 WebRequest

c# - 添加没有外键关系的外键值

uwp - 如何解决 "Failed to connect to device ' 127.0.0. 1' using Universal Authentication"?

visual-studio-2015 - 当我将我的应用程序与商店关联时,Visual Studio 崩溃