首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#实现从FTP上载,出错

2013-03-14 
C#实现从FTP下载,出错string ftpServerIP 192.9.123.123string ftpUserID userstring ftpPasswo

C#实现从FTP下载,出错


        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;
            }
        }


[解决办法]
等待高人出现
[解决办法]
LZ ~错误提示是什么
[解决办法]
你知道错误行号么?你能在异常时直接到调试器的“调用堆栈”去查看各个方法入口时的变量值么?

对于滥用try...catch...的人我只能说:你连系统异常都给自己屏蔽掉了,那么你连调试能力都丧失了。

热点排行