在C#中的FTP服务器检索文件列表 [英] Retrieving a List of Files from an FTP server in C#

查看:151
本文介绍了在C#中的FTP服务器检索文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从FTP服务器检索文件的列表,但我发现了一些奇怪的非ASCII响应。

I'm trying to retrieve a list of files from an FTP server, but I'm getting some weird non-ASCII responses.

下面是我使用的代码:

 public string[] getFileList(string mask)
 {
   if(!logined)
   {
     login();
   }
   Socket cSocket = createDataSocket();
   this.getSslDataStream(cSocket);
   sendCommand("PASV");
   sendCommand("LIST " + "*"+mask);
   stream2.AuthenticateAsClient(remoteHost,
      null,
      System.Security.Authentication.SslProtocols.Ssl3 |
      System.Security.Authentication.SslProtocols.Tls,
      true);
   if(!(retValue == 150 || retValue == 125))
   {
     throw new IOException(reply.Substring(4));
   }
   StringBuilder mes = new StringBuilder();       
   while(true)
   {
     int bytes = cSocket.Receive(buffer, buffer.Length, 0);
     mes.Append(ASCII.GetString(buffer, 0, bytes));
     if(bytes < buffer.Length)
     {
       break;
     }
   }
   string[] seperator = {"\r\n"};
   string[] mess = mes.ToString().Split(seperator, StringSplitOptions.RemoveEmptyEntries);
   cSocket.Close();
   readReply();
   if(retValue != 226)
   {
     throw new IOException(reply.Substring(4));
   }
   return mess;
 }



我从FTP服务器得到的回应是这样的:

The response I get from the FTP server is this:

WRITE:PASV

READ:227 Entering Passive Mode (10,0,2,24,5,119)`

WRITE:LIST *.dat

READ:150 Opening ASCII mode data connection for /bin/ls.

READ:226 Transfer complete.



它停在那里。它返回的字符串数组包含一些非ASCII字符一个索引。看起来像一堆垃圾。也许我的 ASCII.GetString 部分是错的?我不太肯定。

It stops there. The string array that it returns contains one index with some non-ascii characters. Looks like a bunch of garbage. Perhaps my ASCII.GetString part is wrong? I'm not quite sure.

先谢谢了。

推荐答案

我写了一个很容易使用包装库所有的FtpWebRequest的东西。如果你关心检查出来,它在这里 https://gist.github.com/1242616

I wrote a pretty easy to use wrapper library for all the FtpWebRequest stuff. If you care to check it out, it's here https://gist.github.com/1242616

我用它在大量的生产环境,它没有让我失望呢。

I use it in a lot of production environments and it hasn't failed me yet.

这篇关于在C#中的FTP服务器检索文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆