如何简单地访问SkyDrive,写入和读取文件? [英] How to simply access SkyDrive, write and read files?

查看:139
本文介绍了如何简单地访问SkyDrive,写入和读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用SkyDrive备份一些信息.

I want to use SkyDrive to backup some information.

但是似乎他们已经从新的SDK中删除了该命名空间Microsoft.Live.Controls;,并且所有代码示例以及此处的答案均已过时.

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

参考也已过时;没有更多的LiveConnectClient

this reference also is outdated; there is no more LiveConnectClient

这些更改后如何简单地将文件备份到SkyDrive?

(不胜感激任何代码示例或参考.)

(any code sample or reference is appreciated.)

推荐答案

这并不难,但实际上没有参考文献或教程.下面的所有内容在我的Windows Phone 8项目中都可以正常工作.

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.

在安装Live SDK之后,您需要包括Microsoft.Live名称空间.

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.

假设您已经为要发送的文件获得了Stream(我是通过Windows Phone 8上的WinRT文件API获得的,IStorageFolder,然后是文件,然后是file.OpenStreamForReadAsync()),我出于示例目的,我们仅假设它是一个文本文件:

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);
}

实际上,我使用的重载也接受CancellationTokenIProgress<LiveOperationProgress>,主要用于进度通知.

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

在那里,应该将文件上传到登录用户的SkyDrive上的主目录中.

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

这篇关于如何简单地访问SkyDrive,写入和读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆