search file name in ftp server

0 thích 0 không thích
12 lượt xem
đã hỏi 28 Tháng 4, 2018 bởi nguyenthao (9,000 điểm)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace get_list_file_from_ftp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<FileName> sourceFileList = new List<FileName>();
            string sourceURI = "ftp://192.168.0.3/Hinh_NV/";
            //ftp://NguyenThao:123456@192.168.0.3/         
            string sourceUser = "nguyenthao";
            string sourcePass = "123456";

            getFileList(sourceURI, sourceUser, sourcePass, ref sourceFileList);

            var data = from f in sourceFileList where f.fName.Contains(textBox1.Text) select f;

            if (data.Count() > 0)
            {
                string file_a = data.FirstOrDefault().fName;

                MessageBox.Show(file_a);
            }
            else
            {

                MessageBox.Show("khong tim thay");
            }

        }
        public static void getFileList(string sourceURI, string sourceUser, string sourcePass, ref List<FileName> sourceFileList)
        {
            string line = "";
            FtpWebRequest sourceRequest;
            sourceRequest = (FtpWebRequest)WebRequest.Create(sourceURI);
            sourceRequest.Credentials = new NetworkCredential(sourceUser, sourcePass);
            sourceRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            sourceRequest.UseBinary = true;
            sourceRequest.KeepAlive = false;
            sourceRequest.Timeout = -1;
            sourceRequest.UsePassive = true;
            FtpWebResponse sourceRespone = (FtpWebResponse)sourceRequest.GetResponse();
            //Creates a list(fileList) of the file names
            using (Stream responseStream = sourceRespone.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    line = reader.ReadLine();
                    while (line != null)
                    {
                        var fileName = new FileName
                        {
                            fName = line
                        };
                        sourceFileList.Add(fileName);
                        line = reader.ReadLine();
                    }
                }
            }
        }

        public class FileName : IComparable<FileName>
        {
            public string fName { get; set; }
            public int CompareTo(FileName other)
            {
                return fName.CompareTo(other.fName);
            }

        }
    }
}
Looking for an answer?  Share this question:     

Xin vui lòng đăng nhập hoặc đăng ký để trả lời câu hỏi này.

...