Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# OneDrive上载/下载到指定目录_C#_Live Sdk_Onedrive_Live Connect Sdk - Fatal编程技术网

C# OneDrive上载/下载到指定目录

C# OneDrive上载/下载到指定目录,c#,live-sdk,onedrive,live-connect-sdk,C#,Live Sdk,Onedrive,Live Connect Sdk,我正在尝试使用Live SDK(v5.6)在我的Windows Phone 8.1 Silverlight应用程序中包括从OneDrive进行备份/恢复。我可以读/写标准的“me/skydrive”目录,但在找到上传/下载到指定目录的方法时,我经历了一段可怕的时光。我可以创建文件夹,如果它不存在没有问题 我一直在尝试下面没有运气 var res = await _client.UploadAsync("me/skydrive/mydir", fileName, isoStoreFileStrea

我正在尝试使用Live SDK(v5.6)在我的Windows Phone 8.1 Silverlight应用程序中包括从OneDrive进行备份/恢复。我可以读/写标准的“me/skydrive”目录,但在找到上传/下载到指定目录的方法时,我经历了一段可怕的时光。我可以创建文件夹,如果它不存在没有问题

我一直在尝试下面没有运气

var res = await _client.UploadAsync("me/skydrive/mydir", fileName, isoStoreFileStream, OverwriteOption.Overwrite);
我还尝试获取目录ID并将其传递给其他人

var res = await _client.UploadAsync("me/skydrive/" + folderId, fileName, isoStoreFileStream, OverwriteOption.Overwrite);
同样的错误。。我收到“mydir”或该id不受支持

{请求\ url\无效:Microsoft.Live.LiveConnectException:url包含不受支持的路径“mydir”


有什么建议吗?如果您建议uploadasync的答案,您还可以包括我如何从指定目录下载我的文件吗?谢谢!

这里有一个扩展方法,用于检查是否创建了文件夹,以及:

  • 如果已创建,则返回文件夹id
  • 如果未创建,则创建它并返回文件夹id
  • 然后,您可以使用此id上载到该文件夹并从中下载

    public async static Task<string> CreateDirectoryAsync(this LiveConnectClient client,
    string folderName, string parentFolder)
        {
            string folderId = null;
    
            // Retrieves all the directories.
            var queryFolder = parentFolder + "/files?filter=folders,albums";
            var opResult = await client.GetAsync(queryFolder);
            dynamic result = opResult.Result;
    
            foreach (dynamic folder in result.data)
            {
                // Checks if current folder has the passed name.
                if (folder.name.ToLowerInvariant() == folderName.ToLowerInvariant())
                {
                    folderId = folder.id;
                    break;
                }
            }
    
            if (folderId == null)
            {
                // Directory hasn't been found, so creates it using the PostAsync method.
                var folderData = new Dictionary<string, object>();
                folderData.Add("name", folderName);
                opResult = await client.PostAsync(parentFolder, folderData);
                result = opResult.Result;
    
                // Retrieves the id of the created folder.
                folderId = result.id;
            }
    
            return folderId;
        }
    
    通过YnotDraw添加以完成答案

    使用您提供的内容,以下是如何通过指定文件名下载文本文件。下面不包括文件是否未找到以及其他潜在的异常,但以下是星形正确对齐时的工作方式:

    public async static Task<string> DownloadFileAsync(this LiveConnectClient client, string directory, string fileName)
        {
            string skyDriveFolder = await OneDriveHelper.CreateOrGetDirectoryAsync(client, directory, "me/skydrive");
            var result = await client.DownloadAsync(skyDriveFolder);
    
            var operation = await client.GetAsync(skyDriveFolder + "/files");
    
            var items = operation.Result["data"] as List<object>;
            string id = string.Empty;
    
            // Search for the file - add handling here if File Not Found
            foreach (object item in items)
            {
                IDictionary<string, object> file = item as IDictionary<string, object>;
                if (file["name"].ToString() == fileName)
                {
                    id = file["id"].ToString();
                    break;
                }
            }
    
            var downloadResult= await client.DownloadAsync(string.Format("{0}/content", id));
    
            var reader = new StreamReader(downloadResult.Stream);
            string text = await reader.ReadToEndAsync();
            return text;
        }
    

    感谢您发布代码。我看到错误“Microsoft.Live.LiveConnectClient”不包含“DownloadAsync”的定义。有人能告诉我为什么我会看到错误吗?@ezaspi您使用的是最新版本的Live SDK吗?您针对的是哪个平台?Live SDK 5.6.Windows Phone 8.1.是8.1 Silverlight还是WinRT?Personally我还没有在8.1上测试,但是如果没有DownloadAsync,它应该有一个不同的名称。WinRT。我让它工作了。我安装了5.6,但DownloadAsync不可用。所以我使用了CreateBackgroundDownloadAsync。
    LiveOperationResult result = await liveConnectClient.UploadAsync(skyDriveFolder, fileName,
                                                      fileStream, OverwriteOption.Overwrite);
    
    public async static Task<string> DownloadFileAsync(this LiveConnectClient client, string directory, string fileName)
        {
            string skyDriveFolder = await OneDriveHelper.CreateOrGetDirectoryAsync(client, directory, "me/skydrive");
            var result = await client.DownloadAsync(skyDriveFolder);
    
            var operation = await client.GetAsync(skyDriveFolder + "/files");
    
            var items = operation.Result["data"] as List<object>;
            string id = string.Empty;
    
            // Search for the file - add handling here if File Not Found
            foreach (object item in items)
            {
                IDictionary<string, object> file = item as IDictionary<string, object>;
                if (file["name"].ToString() == fileName)
                {
                    id = file["id"].ToString();
                    break;
                }
            }
    
            var downloadResult= await client.DownloadAsync(string.Format("{0}/content", id));
    
            var reader = new StreamReader(downloadResult.Stream);
            string text = await reader.ReadToEndAsync();
            return text;
        }
    
    var result = await DownloadFile(_client, "MyDir", "backup.txt");