【发布时间】:2017-07-08 00:00:30
【问题描述】:
我找不到任何概述使用 OneDrive 在 C# 中跨设备存储和保持应用程序文件同步的正确方法的文档
我已阅读OneDrive Dev Center 的文档,但我不理解 http 代码。 (仅限自学 C#)。
我有点理解我使用 delta 方法从 OneDrive 获取更改的文件,然后在本地保存,但我不知道具体如何,所以通过使用 @987654323 手动检查本地与 OneDrive 来解决它@ 方法。 与 API 中可能处理得更好的实现相比,我当前的实现(以下供参考)似乎相当笨拙。
另外,好像没有反向'delta'函数?也就是说,我在本地将文件写入应用程序,然后告诉 OneDrive 同步更改。那是因为我需要使用PutAsync<> 方法实际上传它吗? (目前我在做什么)
public async Task<T> ReadFromXML<T>(string gamename, string filename)
{
string filepath = _appFolder + @"\" + gamename + @"\" + filename + ".xml";
T objectFromXML = default(T);
var srializer = new XmlSerializer(typeof(T));
Item oneDItem = null;
int casenum = 0;
//_userDrive is the IOneDriveClient
if (_userDrive != null && _userDrive.IsAuthenticated)
{
try
{
oneDItem = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Request().GetAsync();
if (oneDItem != null) casenum += 1;
}
catch (OneDriveException)
{ }
}
StorageFile localfile = null;
try
{
localfile = await ApplicationData.Current.LocalFolder.GetFileAsync(filepath);
if (localfile != null) casenum += 2;
}
catch (FileNotFoundException)
{ }
switch (casenum)
{
case 0:
//neither exist. Throws exception to tbe caught by the calling method, which should then instantiate a new object of type <T>
throw new FileNotFoundException();
case 1:
//OneDrive only - should copy the stream to a new local file then return the object
StorageFile writefile = await ApplicationData.Current.LocalFolder.CreateFileAsync(filepath, CreationCollisionOption.ReplaceExisting);
using (var newlocalstream = await writefile.OpenStreamForWriteAsync())
{
using (var oneDStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync())
{
oneDStream.CopyTo(newlocalstream);
}
}
using (var newreadstream = await writefile.OpenStreamForReadAsync())
{ objectFromXML = (T)srializer.Deserialize(newreadstream); }
break;
case 2:
//Local only - returns the object
using (var existinglocalstream = await localfile.OpenStreamForReadAsync())
{ objectFromXML = (T)srializer.Deserialize(existinglocalstream); }
break;
case 3:
//Both - compares last modified. If OneDrive, replaces local data then returns the object
var localinfo = await localfile.GetBasicPropertiesAsync();
var localtime = localinfo.DateModified;
var oneDtime = (DateTimeOffset)oneDItem.FileSystemInfo.LastModifiedDateTime;
switch (oneDtime > localtime)
{
case true:
using (var newlocalstream = await localfile.OpenStreamForWriteAsync())
{
using (var oneDStream = await _userDrive.Drive.Special.AppRoot.ItemWithPath(filepath).Content.Request().GetAsync())
{ oneDStream.CopyTo(newlocalstream); }
}
using (var newreadstream = await localfile.OpenStreamForReadAsync())
{ objectFromXML = (T)srializer.Deserialize(newreadstream); }
break;
case false:
using (var existinglocalstream = await localfile.OpenStreamForReadAsync())
{ objectFromXML = (T)srializer.Deserialize(existinglocalstream); }
break;
}
break;
}
return objectFromXML;
}
【问题讨论】:
-
不确定您要做什么。 Windows 10 有一个单一驱动器文件夹,您可以将其设置为已在本地同步文件。
-
据我所知,该文件夹在手机设备上不可用,只能在桌面设备(可能还有平板电脑,但我还没有验证)上可用。尽管如此,目标是将数据保存到本地设备,将文件同步回 OneDrive,以便其他设备可以保持最新(并执行相同的操作)。数据文件太大,无法使用
RoamingData。 -
如果您愿意,可以使用一个 c# sdk。 github.com/onedrive/onedrive-sdk-csharp 要记住的一件事是一个驱动器可以存储文件的最新版本。如果您需要在代码中进行任何同步(即结合本地版本和一个驱动器版本的数据)。