• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#带进度条的文件下载

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
        private long fileLength;
        private long downLength;//已经下载文件大小,外面想用就改成公共属性 
        private static bool stopDown;
        public HttpDownLoad()
        {
            fileLength = 0;
            downLength = 0;
            stopDown = false;
            // 
            //   TODO:   在此处添加构造函数逻辑 
            // 
        }

        ///   <summary> 
        ///   文件下载 
        ///   </summary> 
        ///   <param   name= "url "> 连接 </param> 
        ///   <param   name= "fileName "> 本地保存文件名 </param> 
        ///   <param   name= "progressBar "> 进度条 </param> 
        public void httpDownFile(string url, string fileName, ProgressBar progressBar)
        {
            Label lable = new Label();
            httpDownFile(url, fileName, progressBar, lable);
            lable.Dispose();
        }

        ///   <summary> 
        ///   文件下载 
        ///   </summary> 
        ///   <param   name= "url "> 连接 </param> 
        ///   <param   name= "fileName "> 本地保存文件名 </param> 
        ///   <param   name= "progressBar "> 进度条 </param> 
        ///   <param   name= "label "> 返回已经下载的百分比 </param> 
        public string httpDownFile(string url, string fileName, ProgressBar progressBar, Label label)
        {
            string strState = "No";
            stopDown = false;
            Stream str = null, fs = null;
            try
            {
                //获取下载文件长度 
                fileLength = getDownLength(url);
                downLength = 0;
                if (fileLength > 0)
                {
                    WebClient DownFile = new WebClient();
                    str = DownFile.OpenRead(url);
                    //判断并建立文件 
                    if (createFile(fileName))
                    {
                        byte[] mbyte = new byte[1024];
                        int readL = str.Read(mbyte, 0, 1024);
                        fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
                        //读取流 
                        while (readL != 0)
                        {
                            if (stopDown)
                                break;
                            downLength += readL;//已经下载大小 
                            fs.Write(mbyte, 0, readL);//写文件 
                            readL = str.Read(mbyte, 0, 1024);//读流 
                            progressBar.Value = (int)(downLength * 100 / fileLength);
                            label.Text = progressBar.Value.ToString() + "% ";
                            Application.DoEvents();
                            strState = "OK";
                        }
                        str.Close();
                        fs.Close();
                    }
                }
            }
            catch (Exception)
            {
                if (str != null)
                    str.Close();
                if (fs != null)
                    fs.Close();
            }
            return strState;
        }

        ///   <summary> 
        ///   文件下载 
        ///   </summary> 
        ///   <param   name= "url "> 连接 </param> 
        ///   <param   name= "fileName "> 本地保存文件名 </param> 
        public void httpDownFile(string url, string fileName)
        {
            try
            {
                WebClient DownFile = new WebClient();
                DownFile.DownloadFile(url, fileName);
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
            }
        }

        ///   <summary> 
        ///   获取下载文件大小 
        ///   </summary> 
        ///   <param   name= "url "> 连接 </param> 
        ///   <returns> 文件长度 </returns> 
        private long getDownLength(string url)
        {
            try
            {
                WebRequest wrq = WebRequest.Create(url);
                WebResponse wrp = (WebResponse)wrq.GetResponse();
                wrp.Close();
                return wrp.ContentLength;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
                return 0;
            }
        }

        ///   <summary> 
        ///   建立文件(文件如已经存在,删除重建) 
        ///   </summary> 
        ///   <param   name= "fileName "> 文件全名(包括保存目录) </param> 
        ///   <returns> </returns> 
        private bool createFile(string fileName)
        {
            try
            {
                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }
                Stream s = File.Create(fileName);
                s.Close();
                return true;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
                return false;
            }
        }

        public void downClose()
        {
            stopDown = true;
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C语言:如何给全局变量起一个别名?发布时间:2022-07-18
下一篇:
纯C实现strpossubstrstrspiltstr_trim发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap