How to simply access SkyDrive, write and read files?

杀马特。学长 韩版系。学妹 提交于 2019-12-21 02:54:13

问题


I want to use SkyDrive to backup some information.

But seems they have removed this namespace Microsoft.Live.Controls; from the new SDK, and all code samples and also answers here are outdated.

this reference also is outdated; there is no more LiveConnectClient

How can I simply backup files to SkyDrive after these changes?

(any code sample or reference is appreciated.)


回答1:


It's not that hard, but there really are no references or tutorials. Everything that's below works just fine in my Windows Phone 8 project.

You need to include Microsoft.Live namespace after installing Live SDK.

First you have to create and initialize the client. After that, you log in and can send over some data:

LiveConnectClient client;
var auth = new LiveAuthClient("YourGeneratedKey");
var result = await auth.InitializeAsync(new [] {"wl.basic", "wl.signin", "wl.skydrive_update" });

// If you're not connected yet, that means you'll have to log in.
if(result.Status != LiveConnectSessionStatus.Connected)
{
    // This will automatically show the login screen
    result = await auth.LoginAsync(new [] {"wl.basic", "wl.signin", "wl.skydrive_update" });
}

if(result.Status == LiveConnectSessionStatus.Connected)
{
    client = new LiveConnectClient(result.Session);
}

Maybe the process above could be simplified, but it works for me.

Now you can use the client if everything went as planned. I've managed to successfully upload files from streams.

Let's say you've obtained a Stream to the file you want to send (I got that via WinRT file API on Windows Phone 8, IStorageFolder, then getting the file, then file.OpenStreamForReadAsync()), I'll just assume it's a text file for example purposes:

using(stream)
{
    await client.UploadAsync("me/skydrive", "myfile.txt", stream, OverwriteOption.Overwrite);
}

Actually, I've used the overload that also accepts CancellationToken and IProgress<LiveOperationProgress>, mostly for progress notifications.

There, that should upload the file to the main directory on logged user's SkyDrive.



来源:https://stackoverflow.com/questions/18590031/how-to-simply-access-skydrive-write-and-read-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!