日期:2014-05-18  浏览次数:21099 次

C# FTP LIST
最近用C#写了个FTP客户端
代码如下:
C# code
public void DIR()
        {
            Socket dataSocket = null;
            if (dataSocket==null)
                dataSocket = createDataSocket();
            string message = "";
            if (SendComm("LIST") > 0)
            {
                try
                {
                    while (true)
                    {
                        Array.Clear(buffer, 0, buffer.Length);
                        int bytes = dataSocket.Receive(buffer, buffer.Length, 0);
                        message += Encoding.Default.GetString(buffer, 0, bytes);

                        if (bytes < this.buffer.Length)
                        {
                            break;
                        }
                    }

                    FTPReturn.FtpCode = 1;
                    FTPReturn.FtpMessage = "send command [DIR] ok!\r\n";
                    StatusChange(FTPReturn);

                    message = message.Replace("\0", "");
                    FTPReturn.FtpCode = 1;
                    FTPReturn.FtpMessage = message;
                    StatusChange(FTPReturn);

                    string getResult = readMess();
                    FTPReturn.FtpCode = Int32.Parse(getResult.Substring(0, 3));
                    FTPReturn.FtpMessage = getResult;
                    StatusChange(FTPReturn);
                }
                catch (System.Exception ex)
                {
                    FTPReturn.FtpCode = -1;
                    FTPReturn.FtpMessage = ex.Message.ToString();
                    StatusChange(FTPReturn);
                }
            }
            else
            {
                string getResult = readMess();
                FTPReturn.FtpCode = Int32.Parse(getResult.Substring(0, 3));
                FTPReturn.FtpMessage = "send command [DIR] lose!\r\n";
                StatusChange(FTPReturn);
            }
            dataSocket.Close();
        }
        /// <summary>
        /// 开启另一个Socket来接受数据
        /// </summary>
        /// <returns>socket</returns>
        private Socket createDataSocket()
        {
            Socket socket = null;
            int socketPort = 0;
            string ipData = "";
                SetSendMode(FtpSendMode.pasv);//设置被动传输
                if (FTPReturn.FtpMessage.ToString().IndexOf("227 ") >= 0)
                {
                    int i1 = FTPReturn.FtpMessage.IndexOf('(');
                    int i2 = FTPReturn.FtpMessage.IndexOf(')');
                    ipData = FTPReturn.FtpMessage.Substring(i1 + 1, i2 - 1 - i1);
                    string[] ipMess = new string[6];
                    try
                    {
                        ipMess = ipData.Split(',');
                        socketPort = (int.Parse(ipMess[4]) << 8) + int.Parse(ipMess[5]);
                        FTPReturn.FtpCode = 1;
                        FTPReturn.FtpMessage = "端口已经获得“" + socketPort + "”\r\n";
                        StatusChange(FTPReturn);
                    }
                    catch (System.Exception ex)
                    {
                        FTPReturn.FtpCode = -1;
                        FTPReturn.FtpMessage = ex.Message.ToString();
                        StatusChange(FTPReturn);
                    }
                }
                try
                {
                    socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(FTPAdd, socketPort);
                }
                catch (Exception ex)
                {
                    if (socket != null && socket.Connected) s