c# - WebClient MultiFile download one by one -
public partial class form1 : form { public class filex { public string filename { get; set; } public string downloadstring { get; set; } public bool downloaded { get; set; } } public form1() { initializecomponent(); } webclient downloadclient = new webclient(); list<filex> fileslist = new list<filex>(); public void startdownload() { filex filename1 = new filex(); filename1.filename = "awesomefile1.rar"; filename1.downloadstring = "http://download.thinkbroadband.com/5mb.zip"; filex filename2 = new filex(); filename2.filename = "awesomefile2.rar"; filename2.downloadstring = "http://download.thinkbroadband.com/5mb.zip"; fileslist.add(filename1); fileslist.add(filename2); foreach (var file in fileslist) { downloadfile(file.filename, file.downloadstring); } } public void downloadfile(string filename, string fileurl) { string applicationpath = path.getdirectoryname(application.executablepath); string downloadlocation = applicationpath + "\\tempfiles"; bool exists = system.io.directory.exists(downloadlocation); if (!exists) { system.io.directory.createdirectory(downloadlocation); } downloadclient = new webclient(); uri fileurilink = new uri(fileurl); downloadclient.downloadprogresschanged += downloadclient_downloadprogresschanged; downloadclient.downloadfileasync(fileurilink, downloadlocation + filename); } private void downloadclient_downloadprogresschanged(object sender, downloadprogresschangedeventargs e) { string miaxsize = getfilesize(e.totalbytestoreceive); string minsize = getfilesize(e.bytesreceived); filesize_lbl.text = miaxsize + "/" + minsize; progressbar1.maximum = ((int)e.totalbytestoreceive / 100); progressbar1.value = ((int)e.bytesreceived / 100); if (progressbar1.value == progressbar1.maximum) { updatestate_lbl.text = "complete..."; } else { updatestate_lbl.text = "downloading..."; } } private string getfilesize(double bytecount) { string size = "0 bytes"; if (bytecount >= 1073741824.0) size = string.format("{0:##.##}", bytecount / 1073741824.0) + " gb"; else if (bytecount >= 1048576.0) size = string.format("{0:##.##}", bytecount / 1048576.0) + " mb"; else if (bytecount >= 1024.0) size = string.format("{0:##.##}", bytecount / 1024.0) + " kb"; else if (bytecount > 0 && bytecount < 1024.0) size = bytecount.tostring() + " bytes"; return size; } private void button1_click(object sender, eventargs e) { startdownload(); } }
hi everyone, current code job not way want, downloads both files @ same time , reports progress bar both @ same time... make queue , report each file seperatly 1 after finishes
tried background workers makes more horrible because requiers delegates change nececerry labels, file name..size...1 out of how many files etc...
could proper file download solution please
Comments
Post a Comment