• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
创优生活,专业服务
博客园    首页    新随笔    联系   管理     
[导入]一段显示下载进度条的下载文件代码

VB.NET

Public Shared Sub DownFile(ByVal URL As String, ByVal Filename As String, ByVal Prog As ProgressBar) Dim Myrq As HttpWebRequest = HttpWebRequest.Create(URL) Dim myrp As HttpWebResponse = Myrq.GetResponse Dim totalBytes As Long = myrp.ContentLength Prog.Maximum = totalBytes Dim st As Stream = myrp.GetResponseStream Dim so As Stream = New FileStream(Filename, FileMode.Create) Dim totalDownloadedByte As Long = 0 Dim by(1024) As Byte Dim osize As Integer = st.Read(by, 0, by.Length) While osize > 0 totalDownloadedByte = osize + totalDownloadedByte Application.DoEvents() so.Write(by, 0, osize) Prog.Value = totalDownloadedByte osize = st.Read(by, 0, by.LongLength) End While so.Close() st.Close() End Sub

C#

public static void DownFile( string URL, string Filename, ProgressBar Prog ) { 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; 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; Application.DoEvents(); so.Write(by, 0, osize); Prog.Value = (int)totalDownloadedByte; osize = st.Read(by, 0, (int)by.Length); } so.Close(); st.Close(); }

文章来源:http://dotnet.aspx.cc/article/7107b5f4-ff3a-4890-8414-5eb17d50eabf/read.aspx
posted on 2007-11-14 16:22  草秀于木  阅读(363)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3