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;
}
You can use this method.
You can use this wrapper library.
The relevant code is:
I found the answer i did a little of experimenting and now its displaying the files in the listview, Thank you Robert Harvey♦
This code can be used to get the list of files from the ftp
Here is a nice helper to get all files and folders of an FTP directory:
Found here: http://www.snippetsource.net/Snippet/128/get-all-files-of-an-ftp-directory-in-c