博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WinForm更新文件
阅读量:6831 次
发布时间:2019-06-26

本文共 4228 字,大约阅读时间需要 14 分钟。

参考网上一位朋友代码,略有修改 

1 private System.Threading.Thread thread1;  2   3         public Login()  4         {  5             InitializeComponent();  6   7         }  8   9         private void button2_Click(object sender, EventArgs e) 10         { 11             thread1 = new System.Threading.Thread(new System.Threading.ThreadStart(UploadVserion)); 12             thread1.Start(); 13         } 14  15  16         private void UploadVserion() 17         { 18             CheckForIllegalCrossThreadCalls = false; 19  20             String url = "http://123.4.5.5/WebApplication/"; 21             String[] arr_file = { "1.exe", "1.dll", "1.png", "1.rar", "2.mp3" }; 22  23             try 24             { 25  26                 foreach (String filename in arr_file) 27                 { 28                     //DownloadFile("http://localhost:1928/WebServer/downloader/123.rar", @"C:\123.rar", progressBar1, label1); 29  30                     String fullpath = url + filename; 31                     String savepath = Application.StartupPath +"\\" + filename; 32                     this.label3.Text = "更新文件:" + filename; 33                     DownloadFile(fullpath, savepath, this.progressBar1, label4); 34                 } 35                  36                 this.label3.Text = "更新成功"; 37  38             } 39             catch (System.Exception ex) 40             { 41                 this.label3.Text = ex.Message ; 42             } 43         } 44  45         ///          46         /// c#,.net 下载文件         47         ///          48         /// 下载文件地址        49         /// 下载文件地址     50         /// 下载后的存放地址         51         /// 用于显示的进度条         52         ///  53         private void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1) 54         { 55             decimal percent = 0; 56             try 57             { 58                 System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); 59                 System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); 60                 long totalBytes = myrp.ContentLength; 61                 if (prog != null) 62                 { 63                     prog.Maximum = (int)totalBytes; 64                 } 65                 System.IO.Stream st = myrp.GetResponseStream(); 66                 System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); 67                 long totalDownloadedByte = 0; 68                 byte[] by = new byte[1024]; 69                 int osize = st.Read(by, 0, (int)by.Length); 70                 while (osize > 0) 71                 { 72                     totalDownloadedByte = osize + totalDownloadedByte; 73                     System.Windows.Forms.Application.DoEvents(); 74                     so.Write(by, 0, osize); 75                     if (prog != null) 76                     { 77                         prog.Value = (int)totalDownloadedByte; 78                     } 79                     osize = st.Read(by, 0, (int)by.Length); 80  81                     percent = Math.Round((decimal)totalDownloadedByte / (decimal)totalBytes * 100, 2); 82                     label4.Text = "当前补丁下载进度" + percent.ToString() + "%"; 83                     System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息 84                 } 85                 so.Close(); 86                 st.Close(); 87             } 88             catch (System.Exception ex) 89             { 90                 String a = ex.Message; 91                 throw; 92             } 93         } 94  95         private void button3_Click(object sender, EventArgs e) 96         { 97             DialogResult result = MessageBox.Show("确认取消更新?", "提示···", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); 98             if (result == DialogResult.OK) 99             {100                 //thread1.Interrupt();101 102                 //this.label4.Text = "1";103                 thread1.Abort();104                 thread1.Join();105                 106             }107         }
posted on
2016-01-13 21:18 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/loge/p/5128557.html

你可能感兴趣的文章
Windows Azure 的开源 DNA
查看>>
FreeMarker页面静态化入门
查看>>
pat1040:有几个PAT
查看>>
Python!Are you kidding me?
查看>>
Angularjs1.x 项目结构
查看>>
执行Android项目时指定特定的AVD进行測试
查看>>
MFC窗口去边框、置顶、全屏、激活
查看>>
Perl 杂记
查看>>
列表的LIFO与文件交互
查看>>
nodeJS 中关于 promise 的使用
查看>>
jQuery内容过滤选择器再探究(原创)
查看>>
OpenCV——级联分类器(CascadeClassifier)
查看>>
Ajax 访问 或 获取 IIS 虚拟目录
查看>>
Palindrome POJ 1159 动态规划
查看>>
lua的C库
查看>>
Cocos2d-x Eclipse下程序运行产生错误Effect initCheck() returned -1
查看>>
Lync Server 2010的部署系列(四) outlook无法加入联机会议
查看>>
Windows Server 2012安装SQL 2012
查看>>
MS UC 2013-0-虚拟机-标准化-部署-2-模板机-制作-5
查看>>
最常用的四种数据分析方法
查看>>