使用
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string url = "http://files.cnblogs.com/xe2011/WpfApplication1_webbrowser_transparent.rar";
            bool b =DownloadFile(url, @"D:\Administrator\Desktop\123.7z", progressBar1, label1);
            if (b)
                MessageBox.Show("下载成功");
            else
                MessageBox.Show("下载失败");

}

 

        public bool DownloadFile(string URL, string fileName, System.Windows.Controls.ProgressBar progressBar1)
        {
            try
            {
                System.Net.HttpWebRequest httpWebRequest1 = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                System.Net.HttpWebResponse httpWebResponse1 = (System.Net.HttpWebResponse)httpWebRequest1.GetResponse();
 
                long totalLength = httpWebResponse1.ContentLength;
                if (progressBar1 != null)
                {
                    progressBar1.Maximum = (int)totalLength;
                }
                System.IO.Stream stream1 = httpWebResponse1.GetResponseStream();
                System.IO.Stream stream2 = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
                
                long currentLength = 0;
                byte[] by = new byte[1024];
                int osize = stream1.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    WpfApplication.DispatcherHelper.DoEvents();
                    
                    currentLength = osize + currentLength;
                    stream2.Write(by, 0, osize);
                    if (progressBar1 != null)
                    {
                        progressBar1.Value = (int)currentLength;
                        label1.Content = String.Format("{0} / {1}", BytesToString(currentLength), BytesToString(totalLength));
                    }
                    osize = stream1.Read(by, 0, (int)by.Length);
                }
                
                stream2.Close();
                stream1.Close();
 
                return (currentLength == totalLength); 
            }
            catch 
            {
                return false;
            }
        }

附件列表

最新文章

  1. Windows API 设置窗口下控件Enable属性
  2. Appweb写法
  3. 【Unity3D游戏开发】基础知识之Tags和Layers (三二)[转]
  4. SharePoint Dialog 使用
  5. RedHat6.5网卡问题总结
  6. 对付"反盗链"
  7. LocalDateTime返回的是Local时间
  8. web.xml Attribute "xmlns" was already specified for element "web-app"
  9. Unity 3D Framework Designing(4)——设计可复用的SubView和SubViewModel(Part 1)
  10. 201521123119《Java程序设计》第12周学习总结
  11. jquery的$(selector).each(function(index,element))和$.each(dataresource,function(index,element))的区别
  12. 谈谈 final finally finalize 区别
  13. Hadoop集群的构建和安装
  14. highcharts.js的时间轴折线图
  15. 分布式系统中 Unique ID 的生成方法
  16. Expm 4_2 有向无环图中的最短路径问题
  17. xgboost 参数
  18. JavaEE笔记(七)
  19. java 将Map拷贝到另一个Map对象当中
  20. vue.js 源代码学习笔记 ----- 工具方法 debug

热门文章

  1. hdoj 4450 Draw Something 水题
  2. HDU 5150 Sum Sum Sum 素数
  3. Oil Deposits 搜索 bfs 强联通
  4. POJ 3468 A Simple Problem with Integers (splay tree入门)
  5. tomcat ssi配置及升级导致ssi include错误问题解决
  6. 人类即将进入互联网梦境时代(IDA)
  7. 转 如何在IOS设备中去掉屏幕上的status bar
  8. 微软Silverlight欲攻占iPhone和Android手机
  9. jsp下Kindeditor环境搭建
  10. glassfish3 读不到web程序的jar包