如何将 Onedrive<Item> 的内容写入本地文件
How to write the contents of a Onedrive<Item> to a local file
在我的应用中,我使用 OneDrive 来保持数据同步。我已成功将文件写入 OneDrive,但无法用较新的 OneDrive 数据替换本地过时数据。
我当前的方法在不引发异常的情况下完成,return 与 OneDrive 上的文件包含的文本数据不同。
该方法的目标是将OneDrive文件的修改日期与本地文件进行比较,如果OneDrive较新,则将OndeDrive文件的内容写入本地StorageFile,然后return反序列化。
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);
}
}
}
在我的应用中,我使用 OneDrive 来保持数据同步。我已成功将文件写入 OneDrive,但无法用较新的 OneDrive 数据替换本地过时数据。
我当前的方法在不引发异常的情况下完成,return 与 OneDrive 上的文件包含的文本数据不同。 该方法的目标是将OneDrive文件的修改日期与本地文件进行比较,如果OneDrive较新,则将OndeDrive文件的内容写入本地StorageFile,然后return反序列化。
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);
}
}
}