c# 从ftp上下载文件

private void Button1_Click(object sender, EventArgs e)
        {
            string uri = "ftp://172.17.13.127/";
            string username = "username";
            string password = "pwd";
            var list = FtplistFile(uri,username,password);
            string path = @"D:\Example";
            foreach (var item in list)
            {
                Ftpdownloadfile(Path.Combine(uri, item), Path.Combine(path,item),username,password);
            }

            MessageBox.Show("success");
        }
        private List<string> FtplistFile(string url,string username,string password) //get file name form ftp folder
        {
            FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
            listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
            listRequest.Credentials = new NetworkCredential(username, password);
            List<string> lines = new List<string>();

            using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
            using (Stream listStream = listResponse.GetResponseStream())
            using (StreamReader listReader = new StreamReader(listStream))
            {
                while (!listReader.EndOfStream)
                {
                    lines.Add(listReader.ReadLine());
                }
            }
            List<string> name = new List<string>();
            foreach (var line in lines)
            {
                string[] tokens =
            line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
                string n = tokens[3];
                name.Add(n);
            }
            return name;
        }

        private void Ftpdownloadfile(string to_uri,string path,string username,string password)
        {

            FtpWebRequest request =

                (FtpWebRequest)WebRequest.Create(to_uri);

            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(username, password);

            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (FileStream fs = new FileStream(path, FileMode.Create))
                    {
                        byte[] buffer = new byte[102400];
                        int read = 0;
                        do
                        {
                            read = responseStream.Read(buffer, 0, buffer.Length);
                            fs.Write(buffer, 0, read);
                            fs.Flush();
                        } while (!(read == 0));

                        fs.Flush();
                        fs.Close();
                    }
                }
            }


        }

 

转载于:https://www.cnblogs.com/jack-jun/p/11039679.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值