2011-02-19 33 views
4

說明
下載使用Web客戶端的DownloadFileAsync和利用的下載URL輸入的文本文件的多個文件。DownloadFileAsync多個文件使用WebClient的

問題
我已經使用的方法根本不會下載文件。只是跑步,什麼都不做。它填充列表數組然後退出程序而不下載單個文件。我已經搜索瞭解決方案,但缺乏。然後嘗試在這裏搜索數據庫中的解決方案,結果相同。任何幫助表示讚賞。

問題

  1. 爲什麼這個辦法行不通?
  2. 我該怎麼做才能改善這一點並從中吸取教訓。

代碼
DownloadClass.cs

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<string> list = new List<string>(); 
     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 > 0) 
      { 
       wc.DownloadFileAsync(URI, list[download.downloadcount], Application.StartupPath + "\\" + download.file); 
      } 
      else 
      { 
       Console.WriteLine("Downloading is done."); 
       Environment.Exit(0); 
      } 
     } 
    } 
} 

的Program.cs(主類)

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 < 1) 
      { 
       Console.WriteLine("Usage: {0} <download txtfile>", 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(); 
     } 
    } 
} 
+3

在不工作的應用程序中捕獲(例外)是非常沒有生產力的。 – Foxfire

+0

爲什麼你的領域是'靜態'的原因?你爲什麼使用'struct'? –

+0

幾乎所有東西都是靜態的。不是最好的設計! –

回答

2

除了是壞的設計有很多的問題,導致你的代碼不(或者不正確的工作)。

  1. 您需要確保您的應用程序在下載內容時存在。您目前的應用程序立即退出(您必須等待主下載完成)。
  2. 您的應用程序可能會多次下載相同的文件,但根本不會下載其他文件(當您在訪問靜態對象時以類似多線程的方式使用async = multithreading方式時,您需要完全鎖定對象)BTW:不要使用靜態對象完全可以避免這一點。
  3. 即使更正了2,它仍然可能會多次將相同的文件下載到相同的文件名中,從而導致失敗。

只要你沒有關於多線程的知識,我建議你使用同步方法來避免所有這些問題。

+0

1.)我以爲它確實等到它完成= \ 2)。我看到了。我不想嘗試一種同步方法。試圖得到一個多線程的例子,因爲沒有這樣的例子,如果有我沒有找到任何工作的例子。幾乎沒有人會說話以正確的方式學習。所以這樣的董事會是我唯一的選擇=( – Nightforce2

相關問題