gpt4 book ai didi

c# - 如何将文件从独立存储复制到 skydrive

转载 作者:太空狗 更新时间:2023-10-30 01:06:19 25 4
gpt4 key购买 nike

我需要从我的 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。发布对文件夹数据的请求时,只需注册 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) {
onError(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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com