c# - DownloadFileAsync multiple files using webclient -


description
download multiple files using webclient's downloadfileasync , utilizing text file url input download.

problem
approach have used won't download files @ all. runs , nothing. fills list array quits program without downloading single file. have googled solutions come shorthanded. attempted search solution in database here same results. appreciated.

questions

  1. why approach not work?
  2. what can improve , learn this.

code
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 done.");                 environment.exit(0);             }         }     } } 

program.cs (main class)

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 file line line.             using(streamreader file = new streamreader(format))             {                 while ((line = file.readline())!= null)                 {                     // store urls in list.                     downloadclass.list.add(line);                     counter++;                 }             }             downloadclass.begindownload();         }     } } 

besides being bad design there lots of issues lead code not (or nor correctly working).

  1. you need make sure application lives while downloads something. current app quits right away (you have wait downloading complete in main).
  2. you application may download same file multiple times not download others @ (you need lock object when used in async=multithreading way here when accessing static objects) btw: don't use static objects @ avoid in first place.
  3. even if 2 corrected may still download same file multiple times same filename , fail.

as long have no knowledge multithreading i'd recommend use synchoneous methods avoid problems.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -