我找不到概述了使用OneDrive來存儲和保持在C#中跨設備syncrhonised應用程序文件的正確方式的任何文件正確的使用方法OneDrive API來同步文件
我在OneDrive Dev Center閱讀文檔,但我不明白的http代碼。 (僅自學C#)。
我知道我使用delta方法從OneDrive中獲取已更改的文件,然後在本地保存,但我無法弄清楚如何,所以通過使用GetAsync<>
手動檢查本地vs 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」。 – Lindsay
如果您願意,可以使用c#sdk。 https://github.com/onedrive/onedrive-sdk-csharp要記住的一件事是一個驅動器可以存儲該文件的最新版本。如果您需要進行任何同步(即結合來自本地驅動器和一個驅動器版本的數據),則必須在您的代碼中完成同步。 –