文件损坏使用OpenNETCF.Ftp时

时间:2015-05-14 10:52:22

标签: c# opennetcf

我正在尝试使用OpenNETCF.ftp类从远程服务器下载ftp某个文件 文件大小96Kb 打开它时出现错误,显示Corrupt File

private static int BUFFER_SIZE = 512;
private static int DEFAULT_PORT = 21;

private bool                m_connected     = false;
private FTPMode             m_mode          = FTPMode.Passive;
private int                 m_port = DEFAULT_PORT;
private string              m_host          = "";
private FTPTransferType m_type = FTPTransferType.Binary;
private string              m_uid           = "";
private string              m_pwd           = "";
private Socket              m_cmdsocket     = null;
private bool                m_exceptions    = true;
private byte[]              m_buffer        = new byte[BUFFER_SIZE];
private FTPServerType       m_server = FTPServerType.Unknown;

public void GetFile(string remoteFileName, string localFileName, bool overwrite)
{
    int bytesrecvd = 0;
    using (var output = File.Create(localFileName))
    using (var socket = OpenDataSocket())
    {
        response = SendCommand("RETR " + remoteFileName);
        if (!((response.ID == StatusCode.FileStatusOK) || (response.ID == StatusCode.ConnectionAlreadyOpen)))
        {
            if (!m_exceptions)
           {
               return;
           }
           else
           {
               throw new IOException(response.Text);
           }
       }

   while (true)
   {
       bytesrecvd = socket.Receive(m_buffer,m_buffer.Length, 0);
       output.Write(m_buffer, 0, bytesrecvd);
       response.Text = bytesrecvd.ToString();
       if (bytesrecvd <= 0)
       {
           break;
       }
   }
}

1 个答案:

答案 0 :(得分:0)

很可能您没有为传输设置二进制模式,默认情况下它设置为ASCII。 您需要发出'TYPE I'命令。