c# – 如何将文件从独立存储复制到skydrive
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何将文件从独立存储复制到skydrive,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2476字,纯文字阅读大概需要4分钟。
内容图文
![c# – 如何将文件从独立存储复制到skydrive](/upload/InfoBanner/zyjiaocheng/800/825211e08efb45da880dcfe7757c7fcb.jpg)
我需要将我的WP7应用程序中的数据备份到Skydrive,这个文件是xml文件.我知道如何连接到skydrive以及如何在skydrive上创建文件夹:
try
{
var folderData = new Dictionary<string, object>();
folderData.Add("name", "Smart GTD Data");
LiveConnectClient liveClient = new LiveConnectClient(mySession);
liveClient.PostAsync("me/skydrive", folderData);
}
catch (LiveConnectException exception)
{
MessageBox.Show("Error creating folder: " + exception.Message);
}
但我不知道如何将文件从独立存储复制到skydrive.
我该怎么做?
解决方法:
这很简单,你可以使用liveClient.UploadAsync方法
private void uploadFile(LiveConnectClient liveClient, Stream stream, string folderId, string fileName) {
liveClient.UploadCompleted += onLiveClientUploadCompleted;
liveClient.UploadAsync(folderId, fileName, stream, OverwriteOption.Overwrite);
}
private void onLiveClientUploadCompleted(object sender, LiveOperationCompletedEventArgs args) {
((LiveConnectClient)sender).UploadCompleted -= onLiveClientUploadCompleted;
// notify someone perhaps
// todo: dispose stream
}
您可以从IsolatedStorage获取流并像这样发送它
public void sendFile(LiveConnectClient liveClient, string fileName, string folderID) {
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication()) {
Stream stream = storage.OpenFile(filepath, FileMode.Open);
uploadFile(liveClient, stream, folderID, fileName);
}
}
请注意,上传流时需要使用文件夹ID.由于您正在创建文件夹,因此您可以在完成文件夹创建时获取此ID.发布folderData请求时,只需注册PostCompleted事件即可.
这是一个例子
private bool hasCheckedExistingFolder = false;
private string storedFolderID;
public void CreateFolder() {
LiveConnectClient liveClient = new LiveConnectClient(session);
// note that you should send a "liveClient.GetAsync("me/skydrive/files");"
// request to fetch the id of the folder if it already exists
if (hasCheckedExistingFolder) {
sendFile(liveClient, fileName, storedFolderID);
return;
}
Dictionary<string, object> folderData = new Dictionary<string, object>();
folderData.Add("name", "Smart GTD Data");
liveClient.PostCompleted += onCreateFolderCompleted;
liveClient.PostAsync("me/skydrive", folderData);
}
private void onCreateFolderCompleted(object sender, LiveOperationCompletedEventArgs e) {
if (e.Result == null) {
if (e.Error != null) {
one rror(e.Error);
}
return;
}
hasCheckedExistingFolder = true;
// this is the ID of the created folder
storedFolderID = (string)e.Result["id"];
LiveConnectClient liveClient = (LiveConnectClient)sender;
sendFile(liveClient, fileName, storedFolderID);
}
内容总结
以上是互联网集市为您收集整理的c# – 如何将文件从独立存储复制到skydrive全部内容,希望文章能够帮你解决c# – 如何将文件从独立存储复制到skydrive所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。