我正在尝试进入C#编程,但由于某种原因,我一直试图计算在我的ftp服务器上上传的所有文件。我已经尝试了一些来自stackoverflow的代码
代码:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(server);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(user, password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string names = reader.ReadToEnd();
reader.Close();
response.Close();
return names.Split(
new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).ToList();
他确实连接到了服务器,但只是在一个他能找到的文件中向我显示。
Here you can see the list as a string
There is the file which he found
All files that are uploaded on my server
感谢您浪费时间帮助我
//编辑 Here is my list (I don't understand why there are so many indexes)
答案 0 :(得分:1)
您的代码适用于我,但@Ozug可能是正确的,您的服务器无法使用CRLF行结尾。
更强大(也更有效)的实现是:
List<string> names = new List<string>();
while (!reader.EndOfStream)
{
names.Add(reader.ReadLine());
}
它甚至可以处理CR行结尾。
另见C# class to parse WebRequestMethods.Ftp.ListDirectoryDetails FTP response。