如何显示文件从ftp服务器到本地windows应用程序gridview

如何显示文件从ftp服务器到本地windows应用程序gridview

问题描述:

我已经将我的文件上传到ftb服务器,现在我想在我的本地Windows应用程序中显示该文件gridview
i要在datagridview中显示该文件。

i have uploaded my files to ftb server, now i want to display that files in my local windows application gridview i want to display that files in datagridview.

public List<string> ListFiles()
    {
        // Get the object used to communicate with the server.
        var request = (FtpWebRequest)WebRequest.Create("ftp://ipaddress/Requests/");
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        request.Credentials = new NetworkCredential("username", "password");


        List<string> files = new List<string>();
        using (var response = (FtpWebResponse)request.GetResponse())
        {

            using (var responseStream = response.GetResponseStream())
            {
                var reader = new StreamReader(responseStream);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (string.IsNullOrWhiteSpace(line) == false)
                    {
                        files.Add(line.Split(new[] { ' ', '\t' }).Last());
                    }
                }
                return files;

            }
        }
    }
following is the code on my load form.
FTPItility is my class in which  listfiles is a method
      FTPUtility obj = new FTPUtility();
                List<string> strings = new List<string>();
                dataGridViewRequest.DataSource = obj.ListFiles();


这里是您可以使用的代码。

Here is the code you can use.

以下是FtpUtility的代码:

public class FtpUtility
{
    public string UserName { get; set; }
    public string Password { get; set; }
    public string Path { get; set; }
    public List<string> ListFiles()
    {
        var request = (FtpWebRequest)WebRequest.Create(Path);
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        request.Credentials = new NetworkCredential(UserName, Password);
        List<string> files = new List<string>();
        using (var response = (FtpWebResponse)request.GetResponse())
        {
            using (var responseStream = response.GetResponseStream())
            {
                var reader = new StreamReader(responseStream);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (string.IsNullOrWhiteSpace(line) == false)
                    {
                        var fileName = line.Split(new[] { ' ', '\t' }).Last();
                        if (!fileName.StartsWith("."))
                            files.Add(fileName);
                    }
                }
                return files;
            }
        }
    }
}

strong>这里是代码形式:

And here is the code of form:

我创建了一个FtpUtility的实例,并传递了requiered参数,然后获取文件, (Name,Path)并绑定到网格:

I have created an instance of FtpUtility and passed requiered parameters to it, then get the files and put it in a friendly list(Name, Path) and bind to grid:

private void Form1_Load(object sender, EventArgs e)
{
    this.LoadFiles();
}

public void LoadFiles()
{
    var ftp = new FtpUtility();
    ftp.UserName = "username";
    ftp.Password = "password";
    ftp.Path = "ftp://address";

    this.dataGridView1.DataSource = ftp.ListFiles()
                                        .Select(x => new
                                        {
                                            Name = x, //Name Column
                                            Path = ftp.Path + x   //Path Column
                                        }).ToList();
}