C# Winform下载文件并展现进度条

            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);

           osize = st.Read(by, 0, (int)by.Length);

 在做WIN
FORM程序中经常要动用到提升效果,就要求从服务器上下载最新扔软件程序。怎么着下载呢?代码实现如下:

     float percent = 0;       

       
/// <summary>        
        /// c#,.net
下载文件        
        /// </summary>        
        /// <param
name=”UCR-VL”>下载文件地方</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)
         {

               long totalDownloadedByte = 0;      

                    
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
                     label1.Text = “当前补丁下载进程” +
percent.ToString(卡塔尔国 + “%”;
                     System.Windows.Forms.Application.Do伊芙nts(State of Qatar;
//必得加注这句代码,不然label1将因为循环施行太快而来比不上显示新闻
                
}
                 so.Close();
                 st.Close();
             }
            catch (System.Exception)
             {
                throw;
             }
         }

          {                   

转:http://www.cnblogs.com/Slxj/archive/2011/08/21/2147672.html

              long totalBytes = myrp.ContentLength;        

       while (osize > 0)       

              so.Write(by, 0, osize);          

     /// <summary>          

     /// <param
name=”Filename”>下载后的寄存地点</param>         

           }       

              System.Net.HttpWebResponse myrp =
(System.Net.HttpWebResponse)Myrq.GetResponse();   

          /// </summary>       

         prog.Value = (int)totalDownloadedByte;   

  public void DownloadFile(string URL, string filename,
System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label
label1)     

                  }          

            }     

           if (prog != null)          

        ///       

       /// <param
name=”Prog”>用于呈现的进程条</param>       

         if (prog != null)            

          System.IO.Stream st = myrp.GetResponseStream();          

    }

      try          

     {           

           }

      /// c#,.net 下载文件      

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图