FTP获取文件列表错误 [英] FTP gets file list error
问题描述
private string[] GetAllList(string url)
{
List<string> list = new List<string>();
FtpWebRequest req = (FtpWebRequest)WebRequest.Create(new Uri(url));
req.Credentials = new NetworkCredential(ftpPassword, ftpPassword);
req.Method = WebRequestMethods.Ftp.ListDirectory;
req.UseBinary = true;
req.UsePassive = false;
try
{
using (FtpWebResponse res = (FtpWebResponse)req.GetResponse())
{
using (StreamReader sr = new StreamReader(res.GetResponseStream()))
{
string s;
while ((s = sr.ReadLine()) != null)
{
list.Add(s);
}
}
}
}
catch (Exception ex)
{
throw (ex);
}
return list.ToArray();
}
使用上述方法
s = sr。 ReadLine()
错误消息提示主机强制关闭现有连接,浏览器可以输入路径以获取文件列表目录。如何解决这个问题?
The error message prompts the host to forcibly close an existing connection, and the browser can input the path to obtain a list of files in the directory. How to solve this problem?
请验证我的帐户
推荐答案
我不知道什么是问题,但代码中存在错误的异常处理。你使用try {} catch {throw ex;}如果你使用throw ex异常上下文被删除。您可以重写为try {} catch {throw;},但它与您不使用try / catch相同。因此,在代码中尝试/ catch
阻止是不必要的。
I don't know what is problem but there is bad exception handling in your code. You use try{} catch{throw ex;} If you use throw ex exception context is removed. You can rewrite as try{} catch{throw;} but it is same as you don't use try/catch. So try/catch block in your code is unnecessary.
您可以尝试捕获WebException以查看错误代码和消息。
You can try to catch WebException to see error code and message.
这篇关于FTP获取文件列表错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!