//打开对话框选择文件
private void OpenDialogBox_Click(object sender, RoutedEventArgs e)
{
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "*|*";
openFileDialog.RestoreDirectory = true;
openFileDialog.ShowDialog();
//包含路径的文件名
var fileNames = openFileDialog.FileName;
//文件名
var name = openFileDialog.SafeFileName;
DownLoadBox.Text = fileNames;
DownLoadBox.Tag = name;
}
//点击下载下载文件
private void DownLoadButton_Click(object sender, RoutedEventArgs e)
{
string filePath = DownLoadBox.Text;
string name = DownLoadBox.Tag.ToString();
if (string.IsNullOrEmpty(name)) return;
if (filePath == null) return;
var file = new FileInfo(filePath);
//取到文件的总想长度
long length = file.Length;
#region 添加进度条
var stackpael = new StackPanel()
{
Margin = new Thickness(0, 3, 0, 5)
};
var lable = new System.Windows.Controls.Label();
var progressbar = new ProgressBar()
{
Margin = new Thickness(10, 10, 0, 13),
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Width = 300,
Height = 30,
Maximum = length
};
stackpael.Children.Add(lable);
stackpael.Children.Add(progressbar);
DownLoadStackPanel.Children.Add(stackpael);
//创建实体,以方便多线程传值
Controls control = new Controls();
control.Lable = lable;
control.Name = name;
control.Progressbar = progressbar;
control.Path = filePath;
#endregion
//使用线程池下载文件
ThreadPool.QueueUserWorkItem(new WaitCallback(DownLoad), control);
}
#region 使用线程池下载文件void DownLoad(object control)
/// <summary>
/// 使用线程池下载文件
/// </summary>
/// <param name="control"> 参数</param>
private void DownLoad(object control)
{
var controls = control as Controls;
string name = controls.Name;
string filePath = controls.Path;
System.Windows.Controls.Label lable = controls.Lable;
ProgressBar progressBar = controls.Progressbar;
//存放下载文件的路径
string newPath = Path.GetFullPath("../../File");
if (name == null || filePath == null || lable == null || progressBar == null) return;
//下载
DownLoadFile(name, filePath, newPath, lable, progressBar);
}
#endregion
#region 下载void DownLoadFile(string name, string filePath, string newPath, System.Windows.Controls.Label lable, ProgressBar progressBar)
/// <summary>
/// 下载
/// </summary>
/// <param name="name">文件名</param>
/// <param name="filePath">文件路径</param>
/// <param name="newPath">存放文件的路径</param>
/// <param name="lable">存放文件名的控件</param>
/// <param name="progressBar">进度条</param>
private void DownLoadFile(string name, string filePath, string newPath, System.Windows.Controls.Label lable, ProgressBar progressBar)
{
newPath = newPath + "\\" + Guid.NewGuid() + name;
//FileMode.Create创建文件 FileMode.Open 打开文件
using (Stream so = new FileStream(newPath, FileMode.Create))
{
using (Stream st = new FileStream(filePath, FileMode.Open))
{
byte[] by = new byte[1024 * 1024 * 100];
int osize = st.Read(by, 0, (int)by.Length);
int lengths = osize;
//被主线程调用
Action a = () =>
{
lable.Content = name;
};
this.Dispatcher.BeginInvoke(a);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
lengths += osize;
Action action = () =>
{
//进度条
progressBar.Value = lengths;
};
progressBar.Dispatcher.BeginInvoke(action);
}
}
}
}
#endregion
- C# WPF 解压缩7zip文件 带进度条 sevenzipsharp
vs2013附件 :http://download.csdn.net/detail/u012663700/7427461 C# WPF 解压缩7zip文件 带进度条 sevenzipsharp W ...
- VC下载文件显示进度条
VC下载文件显示进度条 逗比汪星人2009-09-18上传 by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...
- Android开发(24)---安卓中实现多线程下载(带进度条和百分比)
当我们学完java中多线程的下载后,可以将它移植到我们的安卓中来,下面是具体实现源码: DownActivity.java package com.example.downloads; import ...
- VC下载文件 + 显示进度条
在codeproject里找了许久,发现这样一个VC下载文件并显示进度条的源码,于是添加了些中文注释: 1.下载线程函数: UINT DownloadFile(LPVOID pParam) { CWn ...
- webclient下载文件 带进度条
private void button1_Click(object sender, EventArgs e) { doDownload(textBox1.Text.Trim()); } private ...
- UrlDownloadFile, 线程下载文件, 带进度条
unit FileDownLoadThread; interface uses Classes, SysUtils, Windows, ActiveX, UrlMon; const S_ABORT = ...
- libcurl开源库在Win32程序中使用下载文件显示进度条实例
一.配置工程引用libcurl库 #define CURL_STATICLIB #include "curl/curl.h" #ifdef _DEBUG #pragma comme ...
- Android 下载文件 显示进度条
加入两个权限 一个是联网,另一个是读写SD卡 <uses-permission android:name="android.permission.INTERNET">& ...
- axios 如何获取下载文件的进度条
exportFun(){ let _that = this const instance = this.axios.create({ onDownl ...
随机推荐
- 解决Node.js调用fs.renameSync报错的问题(Error: EXDEV, cross-device link not permitted)
2014-08-23 今天开始学习Node.js,在写一个文件上传的功能时候,调用fs.renameSync方法错误 出错代码所在如下: function upload(response,reques ...
- Android Json生成及解析实例
JSON的定义: 一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...
- bp神经网络及matlab实现
本文主要内容包含: (1) 介绍神经网络基本原理,(2) AForge.NET实现前向神经网络的方法,(3) Matlab实现前向神经网络的方法 . 第0节.引例 本文以Fisher的Iris数据集 ...
- HTML系列(二):头部meta元素
有关name: 一.页面关键字 网站关键字:用户通过搜索引擎能搜到该网站的词汇.最好控制在10个以内. 基本语法: <meta name="keywords" content ...
- iframe自适应高度代码
var adjustIframe = function (id) { var iframe = document.getElementById(id) var idoc = iframe.conten ...
- Java并发编程:Thread类的使用介绍
在学习Thread类之前,先介绍与线程相关知识:线程的几种状态.上下文切换,然后接着介绍Thread类中的方法的具体使用. 以下是本文的目录大纲: 一.线程的状态 二.上下文切换 三.Thread类中 ...
- Samba在CentOS下的图形化界面的安装
第一步:构建yum仓库(在此用的是北交大的yum仓库) 打开目录/etc/yum.repos.d下的CentOS-Base.repo文件,此处是我自己建的yum仓库,修改里面的链接地址为北交大的镜像的 ...
- powerdesigner反向MySQL5.1数据库 生成ER图
我用的powerdesigner是15.1版本,数据库是MySQL5.1.57 (1)首先新建一个“PhysicalDataModel”类型的文件,然后点击“Database”->"C ...
- s2sh遇到的问题
一:ids for this class must be manually assigned before calling save() "类名.hbm.xml"映射文件中< ...
- wcf客户端 cookie
public class CookieBehavior:IEndpointBehavior { private string _cookie; #region 构造函数 重载+2 public Coo ...