Kevin Pickett

Kevin Pickett

  • NA
  • 1
  • 4.1k

How do I list files from a url directory into a listview.

Mar 21 2014 4:25 PM
I wrote a simple program in VS 2013 C# to compare files on desktop to files in a web directory.

I am listing the local files in a textbox and want to list the remote files in a textlist with checkboxes. Then place a check next to the files in the textlist so I can then click a button to download them.

It is giving me an Object reference error when trying to add the items from regex to the textlist.

----- Current portion of Windows Forms Code =====

public static string GetDirectoryListingRegexForUrl(string url)
// get the files in the url directory and list in the textlist

{
if (url.Equals("http://website/subfolder/"))
{
return ""<a href=\".*\">(?<name>.*)</a>";
}
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
string html = reader.ReadToEnd();
Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
MatchCollection files = regex.Matches(html);
if (files.Count > 0)
{
foreach (Match files in matches)
{
TextList.Items.Add(files.Groups[1]);
}
throw new NotSupportedException();
}
}
}
}

----------

I have created the textlist in the design phase, I am able to add items to the list in the design window so I believe the textlist is already referenced as an object?
Is this the correct way to do this or is their an easier method. This is one part of many in my forms application.

thanks