一段显示下载进度条的下载文件代码

来源:互联网 发布:数据库bc范式分解问题 编辑:程序博客网 时间:2024/06/05 03:49
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();
            }
 
原创粉丝点击