【发布时间】:2021-07-22 15:31:26
【问题描述】:
我想存储通过 ToRetrieve 方法检索的 zip 文件,并使用 C# 编写的 Windows 应用程序中的 SaveFileDialog 控制器将其存储在本地计算机上。
这是我的代码:
if (!string.IsNullOrEmpty(FileName))
{
APIOrderMethods objAPIOrderMethods = new APIOrderMethods();
saveFileDialog1.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
saveFileDialog1.Title = FileName;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Stream stream = objAPIOrderMethods.ToRetrieve(FileName, ServicelURL, useName, password);
Stream streamToWriteTo = File.Open(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite);
stream.CopyToAsync(streamToWriteTo);
}
label12.ForeColor = Color.Green;
}
但我收到此错误:
例外:'无法访问已关闭的流。'
ToRetrieve获取Zip文件的方法:
public Stream ToRetrieve(string Filename, string serviceURL, string Username, string Password)
{
Stream Result = null;
var _saveDir = System.Configuration.ConfigurationManager.AppSettings["Save"];
try
{
using (HttpClient client = new HttpClient())
{
string url = "https://codeload.github.com/tugberkugurlu/ASPNETWebAPISamples/zip/master";
using (HttpResponseMessage response = client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).Result)
using (Stream streamToReadFrom = response.Content.ReadAsStreamAsync().Result)
{
Result = streamToReadFrom;
}
}
}
catch (Exception ex)
{
throw;
}
return Result;
}
【问题讨论】: