list file

Sep 13, 2012 at 11:14 AM
Edited Sep 13, 2012 at 11:15 AM

Hi,

I wrote a little code with System.Net.FtpClient first time.

using (FtpClient cl = new FtpClient("test", "test", "127.0.0.1"))
{
    cl.FtpLogStream = Console.OpenStandardOutput();
    cl.FtpLogFlushOnWrite = true;

    //can't work correctly
    using (FtpDirectory dir = new FtpDirectory(cl, "/pub"))
    {
       foreach (FtpDirectory d in dir.Directories)
       {
          Console.WriteLine("{0} {1} {2}", d.Name, d.Length, d.LastWriteTime);
       }
       foreach (FtpFile f in dir.Files)
       {
         Console.WriteLine("{0} {1} {2}", f.Name, f.Length, f.LastWriteTime);
       }
     }

     //work correctly
     cl.SetWorkingDirectory("/pub");
     using (FtpDirectory dir = new FtpDirectory(cl, ""))
     {
       foreach (FtpDirectory d in dir.Directories)
       {
          Console.WriteLine("{0} {1} {2}", d.Name, d.Length, d.LastWriteTime);
       }
       foreach (FtpFile f in dir.Files)
       {
         Console.WriteLine("{0} {1} {2}", f.Name, f.Length, f.LastWriteTime);
       }
     }
}

 

Debug info

 

> 220 Serv-U FTP Server v6.4 for WinSock ready...
< AUTH TLS
> 500 'AUTH': command not understood.
< AUTH SSL
> 500 'AUTH': command not understood.
< USER test
> 331 User name okay, need password.
< PASS [omitted for security]
> 230 User logged in, proceed.
< FEAT
> 211-Extension supported
>  CLNT
>  MDTM
>  MDTM YYYYMMDDHHMMSS[+-TZ];filename
>  SIZE
>  SITE PSWD;EXEC;SET;INDEX;ZONE;CHMOD;MSG
>  REST STREAM
>  XCRC filename;start;end
>  MODE Z
>  MLST Type*;Size*;Create;Modify*;Win32.ea*;
> 211 End
< TYPE A
> 200 Type set to A.
< EPSV
> 500 'EPSV': command not understood.
< PASV
> 227 Entering Passive Mode (127,0,0,1,41,133)
< MLSD /pub
> 150 Opening BINARY mode data connection for MLSD.
> Type=dir;Modify=20120913090806;Win32.ea=0x00000010; pub
> 226 Transfer complete.
>
pub 0 2012/9/13 9:08:06
< CWD /pub
> 250 Directory changed to /pub
< EPSV
> 500 'EPSV': command not understood.
< PASV
> 227 Entering Passive Mode (127,0,0,1,41,135)
< MLSD
> 150 Opening BINARY mode data connection for MLSD.
> Type=cdir;Modify=20120913090806;Win32.ea=0x00000010; /pub
> Type=file;Size=0;Modify=20120913090754;Win32.ea=0x00000020; pub01.txt
> Type=file;Size=0;Modify=20120913090801;Win32.ea=0x00000020; pub02.txt
> 226 Transfer complete.
>
pub 0 2012/9/13 9:08:06
pub01.txt 0 2012/9/13 9:07:54
pub02.txt 0 2012/9/13 9:08:01
< QUIT
> 221 Goodbye!

 

Can you tell me the different in the code?

Thank You!

Coordinator
Sep 13, 2012 at 11:25 AM

The problem appears to be with the server.  When executing MLSD /pub, the server is sending the details of the /pub directory instead of the contents of pub. 

Sep 14, 2012 at 6:14 PM

you are right. i use another ftp server,it It works correctly. thanks!

The following code in Chinese environment has some problems.

using (FtpClient cl = new FtpClient("test", "test", "127.0.0.1"))
{   
   cl.SslMode = FtpSslMode.None;   
   cl.FtpLogStream = Console.OpenStandardOutput();
   cl.FtpLogFlushOnWrite = true;

   using (FtpDirectory dir = new FtpDirectory(cl, ""))
   {
       foreach (FtpDirectory d in dir.Directories)
       {
           Console.WriteLine("{0} {1} {2}", d.Name, d.Length, d.LastWriteTime);
       }

       foreach (FtpFile f in dir.Files)
       {
           Console.WriteLine("{0} {1} {2}", f.Name, f.Length, f.LastWriteTime);
       }
   }

   if (cl.DirectoryExists("测试"))
   {
       cl.SetWorkingDirectory("测试");
       Console.WriteLine("***" + cl.CurrentDirectory.FullName);
   }

   if (cl.DirectoryExists("数据"))
   {
       cl.SetWorkingDirectory("数据");
       Console.WriteLine("***" + cl.CurrentDirectory.FullName);
   }
}

log info

> 220 Wing FTP Server ready...
< USER test
> 331 Password required for test
< PASS [omitted for security]
> 230 User test logged in.
< FEAT
> 211-Extension supported
>  PBSZ
>  PROT
>  MDTM
>  MDTM YYYYMMDDHHMMSS;filename
>  SIZE
>  MLSD
>  CLNT
>  UTF8
>  AUTH SSL
>  AUTH TLS
>  OPTS
>  STAT
>  EPRT
>  HELP
>  XCRC "filename" SP EP
>  SITE MSG messagetext
>  SITE PSWD oldpass newpass
>  SITE UZIP filename.zip
>  SITE ZIP filename.zip sourcefile1||sourcefile2||sourcefile3||...
> 211 End
< OPTS UTF8 ON
> 200 UTF8 OPTS ON
< TYPE A
> 200 Type set to A
< EPSV
> 229 Entering Extended Passive Mode (|||1049|)
< MLSD
> 150 Opening data channel for directory list.
> type=file;perm=-rwxrwxrwx;modify=20120912115946;size=6; File01.txt
> type=dir;perm=drwxrwxrwx;modify=20120914163947; test
> type=file;perm=-rwxrwxrwx;modify=20120912115951;size=0; 鏂囦欢02.txt
> type=dir;perm=drwxrwxrwx;modify=20120914123919; 娴嬭瘯
> 226 Transfer ok.
>
test 0 2012/9/14 16:39:47
测试 0 2012/9/14 12:39:19
File01.txt 6 2012/9/12 11:59:46
文件02.txt 0 2012/9/12 11:59:51 < PWD
> 257 "/" is current directory.
< CWD 娴嬭瘯
> 250 CWD command successful. "/濞村鐦? is current directory.
< CWD /
> 250 CWD command successful. "/" is current directory.
< CWD 娴嬭瘯
> 250 CWD command successful. "/濞村鐦? is current directory.
< PWD
> 257 "/濞村鐦? is current directory.
***/娴嬭瘯
< CWD 鏁版嵁
> 250 CWD command successful. "/濞村鐦?閺佺増宓? is current directory.
< CWD /濞村鐦?
> 550 CWD command failed. "/婵炴潙顑堥惁?: directory not found.
< QUIT
> 221 Goodbye.
CWD command failed. "/濞村鐦?: directory not found. -- PRESS ANY KEY TO CLOSE --

 

Coordinator
Sep 14, 2012 at 8:25 PM
Edited Sep 14, 2012 at 8:27 PM

I'm almost certain that this is a character conversion issue. I hope you understand it's difficult for me to try to debug this, I only speak english and very bad french when I'm drunk. It looks like there is definitely a character conversion issue in the control connection reading the server responses. I just pushed up a new revision to the default branch that tries to address this. Please try it out and lets see where that leaves us.

Here's the updated revision db8423064d33.

Sep 15, 2012 at 12:08 PM
Edited Sep 15, 2012 at 12:35 PM

I modified the following code can correctly display Chinese characters in the debug information.

thanks

 

FtpTraceListener.cs

        protected StreamWriter Writer {
            get {
                if (_writer == null && this.OutputStream != null) {
                    //_writer = new StreamWriter(this.OutputStream);
                    _writer = new StreamWriter(this.OutputStream, Text.Encoding.Default);
                }

                return _writer;
            }
            private set {
                _writer = value;
            }
        }
< OPTS UTF8 ON
> 200 UTF8 OPTS ON
< TYPE A
> 200 Type set to A
< EPSV
> 229 Entering Extended Passive Mode (|||1035|)
< MLSD
> 150 Opening data channel for directory list.
> type=file;perm=-rwxrwxrwx;modify=20120912115946;size=6; File01.txt
> type=dir;perm=drwxrwxrwx;modify=20120914163947; test
> type=file;perm=-rwxrwxrwx;modify=20120912115951;size=0; 文件02.txt
> type=dir;perm=drwxrwxrwx;modify=20120914123919; 测试
> 226 Transfer ok.
>
test 0 2012/9/14 16:39:47
测试 0 2012/9/14 12:39:19
File01.txt 6 2012/9/12 11:59:46
文件02.txt 0 2012/9/12 11:59:51
< PWD
> 257 "/" is current directory.
< CWD 测试
> 250 CWD command successful. "/测试" is current directory.
< CWD /
> 250 CWD command successful. "/" is current directory.
< CWD 测试
> 250 CWD command successful. "/测试" is current directory.
< PWD
> 257 "/测试" is current directory.
***/测试
< CWD 数据
> 250 CWD command successful. "/测试/数据" is current directory.
< CWD /测试
> 250 CWD command successful. "/测试" is current directory.
< CWD 数据
> 250 CWD command successful. "/测试/数据" is current directory.
< PWD
> 257 "/测试/数据" is current directory.
***/测试/数据
< QUIT
> 221 Goodbye.
-- PRESS ANY KEY TO CLOSE --

 

ps

thank you for this library, it is the best of ftpclient classes I found.

My English is not very good,only paste code :)

Coordinator
Sep 17, 2012 at 3:04 PM

Thanks, I've updated FtpTraceListener.