Populate Website Directory Listing into ListView Control using C#/CSharp

Directory listings are just HTML pages generated by a web server. Each web server generates these HTML pages in its own way because there is no standard way for a web server to list these directories. 

The web server you'd like to list directories from must have directory browsing turned on to get this HTML representation of the files in its directories. So you can only get the directory listing if the HTTP server wants you to be able to.

[C#]
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace DirectoryListing
{
    public partial class frmMainDownloader : Form
    {
        public frmMainDownloader()
        {
            InitializeComponent();
        }

        public static string GetDirectoryListingRegexForUrl(string url)
        {
            return "<a href=\".*\">(?<name>.*)</a>";
        }

        private void btnFetchUrl_Click(object sender, EventArgs e)
        {
            string url = txtUrl.Text.ToString();
            string fileDir = "";

            if (url.Equals(""))
            {
                MessageBox.Show("Please input the Webpage Url you want to parse.", "Invalid URL", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtUrl.Focus();
                return;
            }

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            ListView list = this.listView1;
            ProgressBar pbar = this.progressBar1;

            //CLEARS THE LISTVIEW ITEMS
            list.Items.Clear();

            //RESET PROGRESSBAR / PERCENTAGE VALUE
            pbar.Value = 1;
            pbar.Minimum = 1;
            pbar.Visible = true;
            lblPercent.Text = "";
            lblPercent.Visible = true;

            this.Refresh();

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    string html = reader.ReadToEnd();

                    Regex regex = new Regex(GetDirectoryListingRegexForUrl(url));
                    MatchCollection matches = regex.Matches(html);

                    pbar.Maximum = matches.Count;
                    pbar.Step = 100;

                    if (matches.Count &gt; 0)
                    {
                        foreach (Match match in matches)
                        {
                            if (match.Success)
                            {
                                fileDir = match.Groups["name"].ToString();

                                if (fileDir != "Parent Directory") //EXCLUDES THE PARENT DIRECTORY
                                {
                                    list.Items.Add(fileDir);
                                    pbar.Value ++;

                                    lblPercent.Text = Convert.ToInt16((((double)pbar.Value / (double)pbar.Maximum) * 100 )).ToString() + " %";
                                }

                                this.Refresh();
                            
                            }
                        }
                    }

                    //HIDE PROGRESSBAR AND PERCENTAGE
                    pbar.Visible = false;
                    lblPercent.Visible = false;
                }
            }
        }

    }
            
}

Comments

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6

How to Minimize/Send an App Icon to System Tray in Visual Basic 6