读书人

C#实现FTP方式上载功能

发布时间: 2012-09-09 09:27:54 作者: rapoo

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

读书人网 >C#

热点推荐