我用下面的程序获得一个FTP服务器上的文件列表,但是我得到的结果和我用cuteftp得到的结果不一样, 少好多文件文件和文件夹。
请教高手是出了什么问题?
       /// <summary>
        /// 获得当前目录的文件列表
        /// </summary>
        /// <returns></returns>
        public string[] GetFileList()
        {
            // 建立链接
            if (!bConnected)
            {
                Connect();
            }
            //建立进行数据连接的socket
            Socket socketData = CreateDataSocket();            //传送命令
            SendCommand("NLST ");   //分析应答代码
            if (!(iReplyCode == 150 || iReplyCode == 125 || iReplyCode == 226))
            {
                MessageBox.Show(strReply.Substring(4));
            }
            //获得结果
            strMsg = "";
            while (true)
            {
                int iBytes = socketData.Receive(buffer, buffer.Length, 0);
                strMsg += CN.GetString(buffer, 0, iBytes);
                if (iBytes < buffer.Length)
                {
                    break;
                }
            }
            char[] seperator = { '\n' };
            string[] strsFileList = strMsg.Replace("\r", "").Split(seperator);
            socketData.Close();//数据socket关闭时也会有返回码
            if (iReplyCode != 226)
            {
                ReadReply();
                if (iReplyCode != 226)
                {
                    MessageBox.Show(strReply.Substring(4));
                }
            }
            return strsFileList;
        }

解决方案 »

  1.   

    我记得网上那个类里有一个地方写错了
    #region public String[] GetFileList(String Mask) // 获得文件列表
            /// <summary>
            /// 获得文件列表
            /// </summary>
            /// <param name="Mask">文件掩码</param>
            /// <returns>文件列表</returns>
            public String[] GetFileList(String Mask)
            {
                Socket ClientSocket = CreateDataSocket();
                SendCommand("NLST " + Mask);
                if (!(RetValue == 150 || RetValue == 125))
                    throw new IOException(Reply.Substring(4));
                Message = String.Empty;
                Thread.Sleep(700);
                while (true)
                {
                    if (ClientSocket.Connected)
                    {
                        int Bytes = ClientSocket.Receive(Bufferfer, Bufferfer.Length, 0);
                        Message += ASCII.GetString(Bufferfer, 0, Bytes);
                        if (Bytes < Bufferfer.Length)
                            break;
                    }
                }
                Char[] sEndPointerator = { '\n' };
                String[] Messages = Message.Split(sEndPointerator);
                ClientSocket.Close();
                ReadReply();
                if (RetValue != 226)
                    throw new IOException(Reply.Substring(4));
                return Messages;
            }
            #endregion