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

C#实现FTP方式下载功能
public static void DownLoadXmlFile(String url, String ftpAccount ,String ftpPassword)
        {
            FtpWebRequest reqFTP = null;
            try
            {
                FileStream outputStream = new FileStream(Application.StartupPath + @"\UpdateList.xml", FileMode.Create);
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                reqFTP.UseBinary = true;
                reqFTP.Credentials = new NetworkCredential(ftpAccount, 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();

                outputStream.Close();

                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("文件下载失败错误为" + ex.Message.ToString(), "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }