gpt4 book ai didi

c# - mscorlib.dll 中出现类型为 "System.ArgumentException"的第一次机会异常

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:39 24 4
gpt4 key购买 nike

  try
{
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp path");
req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
using (var resp = (FtpWebResponse)req.GetResponse())
{
Console.WriteLine(resp.WelcomeMessage);
}
FtpWebResponse res = (FtpWebResponse)req.GetResponse();
Stream rs = res.GetResponseStream();
StreamReader read1 = new StreamReader(res.GetResponseStream());// prob A
Console.WriteLine(read1.ReadToEnd());
Console.WriteLine("Directory is compleate,status(0)",
res.StatusDescription);
read1.Close();
rs.Close();
}
catch (Exception e1)
{
Console.WriteLine(e1.Message);
}

我正在尝试通过 C# 访问 ftp 服务器。

然而,代码错误并出现异常:
“System.ArgumentException”类型的第一次机会异常发生在 mscorlib.dll 中。流不可读。

我们将不胜感激。

最佳答案

将所有 IDisposable 资源放在 using(...) {...} 中。该技术可以防止资源泄漏/关闭资源调用:

try {
FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp path");
req.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

// Response: we're going to work wuth respose within "using" block only
using (FtpWebResponse resp = (FtpWebResponse)req.GetResponse()) {
Console.WriteLine(resp.WelcomeMessage);

// Reader: once again reader's opened once and called within using only
using (StreamReader reader = new StreamReader(resp.GetResponseStream())) {
Console.WriteLine(reader.ReadToEnd());
Console.WriteLine("Directory is complete, status(0)", resp.StatusDescription);
}
}
catch (Exception ex) { // <- Bad idea to catch all possible exceptions without "throw;"
Console.WriteLine(ex.Message);
}

关于c# - mscorlib.dll 中出现类型为 "System.ArgumentException"的第一次机会异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21527999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com