title: DownloadFileAsync multiple files using webclient
body: <p><strong>Description</strong><br>
  Download multiple files using webclient's DownloadFileAsync and utilizing a text file for URL input for download.</p>
  
  <p><strong>Problem</strong><br>
  The approach that I have used won't download files at all. Just runs and does nothing. It fills the list array then quits the program without downloading a single file. I have googled for solutions but come up shorthanded. Then attempted to search for a solution in the database here with same results. Any help is appreciated.</p>
  
  <p><strong>Questions</strong><br></p>
  
  <ol>
  <li>Why does this approach not work?</li>
  <li>What can I do to improve this and learn from this.</li>
  </ol>
  
  <p><strong>Code</strong><br>
  DownloadClass.cs</p>
  
  <pre><code>using System;
  using System.ComponentModel;
  using System.Collections.Generic;
  using System.Net;
  using System.Threading;
  using System.Windows.Forms;
  
  namespace ThreadTest
  {
      class DownloadClass
      {
          public struct download
          {
              public static string URL { get; set; }
              public static string file { get; set; }
              public static string[] link;
              public static int downloadcount;
          }
  
          public static List&lt;string&gt; list = new List&lt;string&gt;();
          public static WebClient wc = new WebClient();
  
          public static void Download()
          {
              int count = 0;
              download.URL = list[0];
              Uri URI = new Uri(download.URL);
              UriBuilder uri = new UriBuilder(URI);
              download.link = uri.Path.ToLower().Split(new char[] { '/' });
  
              count = 0;
              // Find file
              foreach (string abs in download.link)
              {
                  count++;
                  if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
                  {
                      try
                      {
                          download.file = download.link[count];
                          wc.Proxy = null;
                          wc.DownloadFileCompleted += new AsyncCompletedEventHandler(wc_DownloadFileCompleted);
                          wc.DownloadFileAsync(URI, Application.StartupPath + "\\" + download.file);
                          break;
                      }
                      catch (Exception)
                      { }
                  }
              }
          }
  
          public static void BeginDownload()
          {
              new Thread(Download).Start();
          }
  
          public static void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
          {
              int count = 0;
              download.downloadcount++;
              download.URL = list[0];
              Uri URI = new Uri(download.URL);
              UriBuilder uri = new UriBuilder(URI);
  
              download.link = uri.Path.ToLower().Split(new char[] { '/' });
  
              count = 0;
              // Find file
              foreach (string abs in download.link)
              {
                  count++;
                  if (abs.ToLower().Contains(".html") || abs.ToLower().Contains(".exe") || abs.ToLower().Contains(".txt"))
                  {
                      try
                      {
                          download.file = download.link[count];
                      }
                      catch (Exception)
                      { }
                  }
              }
              list.RemoveAt(0);
              if (list.Count &gt; 0)
              {
                  wc.DownloadFileAsync(URI, list[download.downloadcount], Application.StartupPath + "\\" + download.file);
              }
              else
              {
                  Console.WriteLine("Downloading is done.");
                  Environment.Exit(0);
              }
          }
      }
  }
  </code></pre>
  
  <p>Program.cs (Main Class)</p>
  
  <pre><code>using System;
  using System.IO;
  using System.Collections.Generic;
  using System.Windows.Forms;
  
  namespace ThreadTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              if (args.Length &lt; 1)
              {
                  Console.WriteLine("Usage: {0} &lt;download txtfile&gt;", Environment.GetCommandLineArgs()[0]);
                  Environment.Exit(0);
              }
  
              int counter = 0;
              string line;
              string format = string.Format("{0}\\{1}", Application.StartupPath, args[0]);
  
              // Read the file line by line.
              using(StreamReader file = new StreamReader(format))
              {
                  while ((line = file.ReadLine())!= null)
                  {
                      // Store urls in a list.
                      DownloadClass.list.Add(line);
                      counter++;
                  }
              }
              DownloadClass.BeginDownload();
          }
      }
  }
  </code></pre>