我正在从skydrive文件夹下载图片。下载图像时,我需要将其保存在名为“pictures”的文件夹中
但是如何获取下载文件的名称?我尝试了下一个代码,但 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;
}
答案 0 :(得分: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;
}
答案 1 :(得分:0)
实现这一目标的最佳方式是使用后台流程
可以在此处找到示例实现:
http://msdn.microsoft.com/en-us/library/hh202959(v=vs.92).aspx
但这个Skydrive浏览器示例是一个能够更好地满足您需求的解决方案:
http://code.msdn.microsoft.com/MetroSky-A-Complete-4250b80f 要么 https://stackoverflow.com/questions/7346450/download-file-from-skydrive