C# Winform下载文件并显示进度条的实现代码
程序员文章站
2024-02-21 12:54:16
方法一:
效果如下图所示:
代码如下:
using system;
using system.collections.generic;
usi...
方法一:
效果如下图所示:
代码如下:
using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; namespace winshowdown { public partial class frmmain : form { public frmmain() { initializecomponent(); } private void btndown_click(object sender, eventargs e) { downloadfile("http://localhost:1928/webserver/downloader/123.rar", @"c:\123.rar", progressbar1, label1); } /// <summary> /// c#,.net 下载文件 /// </summary> /// <param name="url">下载文件地址</param> /// /// <param name="filename">下载后的存放地址</param> /// <param name="prog">用于显示的进度条</param> /// public void downloadfile(string url, string filename, system.windows.forms.progressbar prog, system.windows.forms.label label1) { float percent = 0; try { system.net.httpwebrequest myrq = (system.net.httpwebrequest)system.net.httpwebrequest.create(url); system.net.httpwebresponse myrp = (system.net.httpwebresponse)myrq.getresponse(); long totalbytes = myrp.contentlength; if (prog != null) { prog.maximum = (int)totalbytes; } system.io.stream st = myrp.getresponsestream(); system.io.stream so = new system.io.filestream(filename, system.io.filemode.create); long totaldownloadedbyte = 0; byte[] by = new byte[1024]; int osize = st.read(by, 0, (int)by.length); while (osize > 0) { totaldownloadedbyte = osize + totaldownloadedbyte; system.windows.forms.application.doevents(); so.write(by, 0, osize); if (prog != null) { prog.value = (int)totaldownloadedbyte; } osize = st.read(by, 0, (int)by.length); percent = (float)totaldownloadedbyte / (float)totalbytes * 100; label1.text = "当前补丁下载进度" + percent.tostring() + "%"; system.windows.forms.application.doevents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 } so.close(); st.close(); } catch (system.exception) { throw; } } } }
实现方法二:
winform下载文件并显示下载进度示例
/// <summary> /// 显示进度 /// </summary> /// <param name="val"></param> private void progressbar_value(int val) { progressbar1.value = val; label1.text = val.tostring() + "%"; } /// <summary> /// 下载文件 /// </summary> /// <param name="url"></param> /// <param name="savefile"></param> /// <param name="downloadprogresschanged"></param> /// <param name="downloadfilecompleted"></param> private void downloadfile(string url, string savefile, action<int> downloadprogresschanged, action downloadfilecompleted) { webclient client = new webclient(); if (downloadprogresschanged != null) { client.downloadprogresschanged += delegate(object sender, downloadprogresschangedeventargs e) { this.invoke(downloadprogresschanged, e.progresspercentage); }; } if (downloadfilecompleted != null) { client.downloadfilecompleted += delegate(object sender, asynccompletedeventargs e) { this.invoke(downloadfilecompleted); }; } client.downloadfileasync(new uri(url), savefile); } delegate void action(); //.net framework 2.0得自定义委托action /// <summary> /// 点击下载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_click(object sender, eventargs e) { downloadfile("http://xiazai.jb51.net/update.zip", @"f:update.zip", progressbar_value, null); }