string ftpServerIP = "192.9.123.123";
        string ftpUserID = "user";
        string ftpPassword = "pwd";
        string fileName = "new.fr3";
        string filePath = @"d:\new";
 public bool getftpfile(string ftpServerIP, string ftpUserID, string ftpPassword, string filePath, string fileName)
        {
            if (checkftpfile(ftpServerIP, ftpUserID, ftpPassword, filePath, fileName) == false)
            {
                return false;
            }
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            FileStream outputStream = new FileStream(filePath +@"\" + fileName,FileMode.Create);            try
            {
                FtpWebRequest reqFTP;                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/IC" + fileName)); 
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                long cl = response.ContentLength;
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();               
                response.Close();
                outputStream.Close();
                return true;
            }
            catch (Exception ex)
            {
                outputStream.Close();
              //  string failed_message = "Failed:"  + "\r\n" + ex.Message + "\r\n" + ex.InnerException.Message;
                MessageBox.Show(ex.Message);
                return false;
            }
            finally
            {
                outputStream.Close();
            }
        }
public bool checkftpfile(string ftpServerIP, string ftpUserID, string ftpPassword, string filePath, string fileName)
        {
            try
            {
                FtpWebRequest reqFTP;                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(@"ftp://" + ftpServerIP + "/IC"));
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = true;
                reqFTP.UsePassive = true;                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();                StreamReader reader = new StreamReader(response.GetResponseStream());                if (reader.ReadToEnd().IndexOf(fileName) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                string failed_message = "Failed" + fileName + "\r\n" + ex.Message + "\r\n" + ex.InnerException.Message;
                MessageBox.Show(failed_message);
                return false;
            }
        }

解决方案 »

  1.   

    你知道错误行号么?你能在异常时直接到调试器的“调用堆栈”去查看各个方法入口时的变量值么?对于滥用try...catch...的人我只能说:你连系统异常都给自己屏蔽掉了,那么你连调试能力都丧失了。
      

  2.   

    我唯一能衷告你的就是你应该删除所有的try..catch...finally,然后学会使用调试器。如果自己都懒得调试了,你拿不出调试器信息来,让别人给胡乱瞎猜错误这是无用的。
      

  3.   

    错误提示是$exception {"远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)。"} System.Exception {System.Net.WebException} 路径什么的都是绝对路径,并且可以获取服务器内的文件列表
      

  4.   

    谢谢大家了。我自己发现错误信息中URI指向的路径少了一个斜杠,问题解决了。