如何使用 C# 在 Windows 应用程序中使用 SaveFileDialog 在来自 HttpClient 的本地计算机上保存 Zip 文件?
How to save Zip file on local machine which is coming from HttpClient using SaveFileDialog in Windows application using C#?
我想存储通过 ToRetrieve
方法检索的 zip 文件,并使用 SaveFileDialog
控制器将其存储在本地计算机上,在 Windows 应用程序中使用 C# 编写。
这是我的代码:
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;
}
但是我得到这个错误:
Exception :'Cannot access a closed Stream.'
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;
}
问题出在方法上,ToRetrieve
。
在 using 语句中使用 streamToReadFrom
,将调用 Dispose
方法。
由于结果包含引用,您将返回一个关闭的流。
我想存储通过 ToRetrieve
方法检索的 zip 文件,并使用 SaveFileDialog
控制器将其存储在本地计算机上,在 Windows 应用程序中使用 C# 编写。
这是我的代码:
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;
}
但是我得到这个错误:
Exception :'Cannot access a closed Stream.'
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;
}
问题出在方法上,ToRetrieve
。
在 using 语句中使用 streamToReadFrom
,将调用 Dispose
方法。
由于结果包含引用,您将返回一个关闭的流。