0

我正在从 skydrive 文件夹下载图像。下载图像后,我需要将其保存在名为“图片”的文件夹中

但是我怎样才能得到下载文件的名称呢?我尝试了下一个代码,但fs返回null

private void download()
        {
            if (ControlBackup_ID != null)
            {               
                foreach (string it in contenidoSkyPic)
                {
                    //MessageBox.Show (it);
                    infoTextBlock3.Text = "Downloading backup pictures..wait...";

                    client.DownloadAsync(it + "/content");
                    client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadCompleted);
                }               
            }
            else
                MessageBox.Show("Backup file of pictures doesn't exist!", "Error", MessageBoxButton.OK);
        }



void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
             Stream stream = e.Result; //Need to write this into IS
             FileStream fs = stream as FileStream;

             if (fs != null)
             {
                 try
                 {
                     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                     {
                         using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("pictures\\" + fs.Name, FileMode.Create))
                         {
                             stream.CopyTo(fileStream);
                             cantImatges_progreso_down += 1;
                         }
                     }
                 }
                 catch { }

                 if (cantImatges_progreso_down == contenidoSkyPic.Count())
                 {
                     infoTextBlock3.Text = "Restore pictures completed!";
                 }
             }

        }
        else
        {
            // process error  
            MessageBox.Show("Restore pictures failed.", "Failure", MessageBoxButton.OK);
        }

        client.DownloadCompleted -= client_DownloadCompleted;            


    }
4

2 回答 2

1

最后,我找到了这个解决方案。我已经看到我可以使用“ userstate ”来传递文件名

解决方案:

当我扫描 skydrive 文件夹时,现在我存储 ID 和文件名:

List<KeyValuePair<string, string>> contenidoSkyPic ;

 void getFilesImatges_GetCompleted(object sender, LiveOperationCompletedEventArgs e)
 {
     List<object> data = (List<object>)e.Result["data"];

            contenidoSkyPic = new List<KeyValuePair<string, string>>();
            contenidoSkyPic.Clear();


            foreach (IDictionary<string, object> content in data)
            {
                contenidoSkyPic.Add(new KeyValuePair<string, string>((string)content["id"], (string)content["name"]));   
            }


}

然后,“下载”将是:

private void download()
        {
            if (ControlBackup_ID != null)
            {               
                foreach (string it in contenidoSkyPic)
                {
                    //MessageBox.Show (it);
                    infoTextBlock3.Text = "Downloading backup pictures..wait...";


LiveConnectClient client = new LiveConnectClient(session);
client.DownloadCompleted += new EventHandler<LiveDownloadCompletedEventArgs>(client_DownloadImatgesCompleted);
client.DownloadAsync(it.Key + "/content", it.Value );

                }               
            }
            else
                MessageBox.Show("Backup file of pictures doesn't exist!", "Error", MessageBoxButton.OK);
        }

当下载完成时调用 client_DownloadCompleted 并且我可以获得每个文件的名称:

void client_DownloadCompleted(object sender, LiveDownloadCompletedEventArgs e)
    {
        if (e.Error == null)
        {
             Stream stream = e.Result; //Need to write this into IS
            string _namePicture = e.UserState.ToString();


                 try
                 {
                     using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
                     {
                         using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("pictures\\" + _namePicture , FileMode.Create))
                         {
                             stream.CopyTo(fileStream);
                             cantImatges_progreso_down += 1;
                         }
                     }
                 }
                 catch { }

                 if (cantImatges_progreso_down == contenidoSkyPic.Count())
                 {
                     infoTextBlock3.Text = "Restore pictures completed!";
                 }
             }


        client.DownloadCompleted -= client_DownloadCompleted;            


    }
于 2012-06-02T09:40:30.073 回答
0

实现这一点的最佳方法是使用后台进程

可以在此处找到示例实现:

http://msdn.microsoft.com/en-us/library/hh202959(v=vs.92).aspx

但更适合您需求的解决方案是这个 Skydrive 浏览器示例:

http://code.msdn.microsoft.com/MetroSky-A-Complete-4250b80fhttps://stackoverflow.com/questions/7346450/download-file-from-skydrive

于 2012-05-30T15:41:01.897 回答