List all files from online FTP directory to a list

2019-03-20 03:06发布

How could i and everyone else who is reading this list all files from online directory to a listview?

This is the code for a local directory to be listed i would like to know if there was a way to make it so that is connects to a FTP website and lists files?

FolderBrowserDialog folderPicker = new FolderBrowserDialog();
if (folderPicker.ShowDialog() == DialogResult.OK)
{

    ListView1.Items.Clear();

    string[] files = Directory.GetFiles(folderPicker.SelectedPath);
    foreach (string file in files)
    {

        string fileName = Path.GetFileNameWithoutExtension(file);
        ListViewItem item = new ListViewItem(fileName);
        item.Tag = file;

        ListView1.Items.Add(item);

    }

}

I have used this code but i cannot seem to get it to work its not coming up with an error but its not listing the files on the webserver either?

private void ConnectBtn_Click(object sender, EventArgs e)
    {
        ListDirectory();
    }

    public string[] ListDirectory()
    {
        var list = new List<string>();

        var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory);

        using (var response = (FtpWebResponse)request.GetResponse())
        {
            using (var stream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream, true))
                {
                    while (!reader.EndOfStream)
                    {
                        list.Add(reader.ReadLine());
                    }
                }
            }
        }

        return list.ToArray();
    }

    private FtpWebRequest createRequest(string uri, string method)
    {
        var r = (FtpWebRequest)WebRequest.Create(uri);

        r.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
        r.Method = method;

        return r;
    }

标签: c# listview ftp
5条回答
淡お忘
2楼-- · 2019-03-20 03:21

You can use this method.

    public static string[] GetFiles(string path, NetworkCredential Credentials, SearchOption searchOption)
    {
        var request = (FtpWebRequest)WebRequest.Create(path);
        request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
        request.Credentials = Credentials;
        List<string> files = new List<string>();
        using (var response = (FtpWebResponse)request.GetResponse())
        {
            using (var responseStream = response.GetResponseStream())
            {
                var reader = new System.IO.StreamReader(responseStream);
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (string.IsNullOrWhiteSpace(line) == false)
                    {
                        if (!line.Contains("<DIR>"))
                        {
                            string[] details = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                            string file = line.Replace(details[0], "")
                                .Replace(details[1], "")
                                .Replace(details[2], "")
                                .Trim();
                            files.Add(file);
                        }
                        else
                        {
                            if (searchOption == SearchOption.AllDirectories)
                            {
                                string dirName = line.Split(
                                        new string[] { "<DIR>" },
                                        StringSplitOptions.RemoveEmptyEntries
                                        ).Last().Trim();
                                string dirFullName = String.Format("{0}/{1}", path.Trim('/'), dirName);
                                files.AddRange(GetFiles(dirFullName, Credentials, searchOption));
                            }
                        }
                    }
                }
            }
        }
        return files.ToArray();
    }
查看更多
一纸荒年 Trace。
3楼-- · 2019-03-20 03:36

You can use this wrapper library.

The relevant code is:

    public string[] ListDirectory() {
        var list = new List<string>();

        var request = createRequest(WebRequestMethods.Ftp.ListDirectory);

        using (var response = (FtpWebResponse)request.GetResponse()) {
            using (var stream = response.GetResponseStream()) {
                using (var reader = new StreamReader(stream, true)) {
                    while (!reader.EndOfStream) {
                        list.Add(reader.ReadLine());
                    }
                }
            }
        }

        return list.ToArray();
    }
查看更多
神经病院院长
4楼-- · 2019-03-20 03:40

I found the answer i did a little of experimenting and now its displaying the files in the listview, Thank you Robert Harvey♦

private void ConnectBtn_Click(object sender, EventArgs e)
    {
        ListDirectory();
    }

    public string[] ListDirectory()
    {
        var list = listView1;

        var request = createRequest(TxtServer.Text, WebRequestMethods.Ftp.ListDirectory);

        using (var response = (FtpWebResponse)request.GetResponse())
        {
            using (var stream = response.GetResponseStream())
            {
                using (var reader = new StreamReader(stream, true))
                {
                    while (!reader.EndOfStream)
                    {
                        list.Items.Add(reader.ReadLine());
                    }
                }
            }
        } List<string> l = new List<string>();
        return l.ToArray();
    }

    private FtpWebRequest createRequest(string uri, string method)
    {
        var r = (FtpWebRequest)WebRequest.Create(uri);

        r.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
        r.Method = method;

        return r;
    }
查看更多
姐就是有狂的资本
5楼-- · 2019-03-20 03:44

This code can be used to get the list of files from the ftp

private void ftpFileRead()
{
  FtpWebRequest Request = (FtpWebRequest)WebRequest.Create("your ftpAddress");
  Request.Method = WebRequestMethods.Ftp.ListDirectory;     
  Request.Credentials = new NetworkCredential(your ftp username,your ftp password);
  FtpWebResponse Response = (FtpWebResponse)Request.GetResponse();
  Stream ResponseStream = Response.GetResponseStream();
  StreamReader Reader = new StreamReader(ResponseStream);

  ListBox1.Items.Add(Response.WelcomeMessage);
  while (!Reader.EndOfStream)//Read file name   
  {
    ListBox1.Items.Add(Reader.ReadLine().ToString());
  }
  Response.Close();
  ResponseStream.Close();
  Reader.Close();
}
查看更多
太酷不给撩
6楼-- · 2019-03-20 03:45

Here is a nice helper to get all files and folders of an FTP directory:

public static IEnumerable<string> GetFilesInFtpDirectory(string url, string username, string password)
{
    // Get the object used to communicate with the server.
    var request = (FtpWebRequest)WebRequest.Create(url);
    request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    request.Credentials = new NetworkCredential(username,password);

    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)
                {
                    yield return line.Split(new[] { ' ', '\t' }).Last();    
                }
            }
        }
    }
}

Found here: http://www.snippetsource.net/Snippet/128/get-all-files-of-an-ftp-directory-in-c

查看更多
登录 后发表回答