using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace CopyProgress015
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thdCopyFile; //创建一个线程
private string str = ""; //用来记录源文件的名字
FileStream FormerOpenStream; //实例化源文件FileStream类
FileStream ToFileOpenStream; //实例化目标文件FileStream类 #region //复制文件 函数
/// <summary>
/// 复制文件
/// </summary>
/// <param name="FormerFile">源文件路径</param>
/// <param name="ToFile">目的文件路径</param>
/// <param name="TranSize">传输大小</param>
/// <param name="progressBar1">ProgressBar控件</param>
public void CopyFile(string FormerFile, string ToFile, int TranSize, ProgressBar progressBar1)
{
progressBar1.Value = ;//设置进度条的当前位置为0
progressBar1.Minimum = ; //设置进度条的最小值为0
try
{
FormerOpenStream = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
return;
}
try
{
FileStream fileToCreate = new FileStream(ToFile, FileMode.Create); //创建目的文件,如果已存在将被覆盖
fileToCreate.Close();//关闭所有fileToCreate的资源
fileToCreate.Dispose();//释放所有fileToCreate的资源
}
catch(IOException ex)
{
MessageBox.Show(ex.Message);
return;
} ToFileOpenStream = new FileStream(ToFile, FileMode.Append, FileAccess.Write);//以写方式打开目的文件 int max = Convert.ToInt32(Math.Ceiling((Double)FormerOpenStream.Length / (Double)TranSize));//根据一次传输的大小,计算最大传输个数. Math.Ceiling 方法 (Double),返回大于或等于指定的双精度浮点数的最小整数值。
progressBar1.Maximum = max;//设置进度条的最大值
int FileSize; //每次要拷贝的文件的大小
if (TranSize < FormerOpenStream.Length) //如果分段拷贝,即每次拷贝内容小于文件总长度
{
byte[] buffer = new byte[TranSize]; //根据传输的大小,定义一个字节数组,用来存储传输的字节
int copied = ;//记录传输的大小
int tem_n = ;//设置进度栏中进度的增加个数
while (copied <= ((int)FormerOpenStream.Length - TranSize))
{
FileSize = FormerOpenStream.Read(buffer, , TranSize);//从0开始读到buffer字节数组中,每次最大读TranSize
FormerOpenStream.Flush(); //清空缓存
ToFileOpenStream.Write(buffer, , TranSize); //向目的文件写入字节
ToFileOpenStream.Flush();//清空缓存
ToFileOpenStream.Position = FormerOpenStream.Position; //是源文件的目的文件流的位置相同
copied += FileSize; //记录已经拷贝的大小
progressBar1.Value = progressBar1.Value + tem_n; //增加进度栏的进度块
}
int leftSize = (int)FormerOpenStream.Length - copied; //获取剩余文件的大小
FileSize = FormerOpenStream.Read(buffer, , leftSize); //读取剩余的字节
FormerOpenStream.Flush();
ToFileOpenStream.Write(buffer, , leftSize); //写入剩余的部分
ToFileOpenStream.Flush();
}
else //如果整体拷贝,即每次拷贝内容大于文件总长度
{
byte[] buffer = new byte[FormerOpenStream.Length];
FormerOpenStream.Read(buffer, , (int)FormerOpenStream.Length);
FormerOpenStream.Flush();
ToFileOpenStream.Write(buffer, , (int)FormerOpenStream.Length);
ToFileOpenStream.Flush();
}
FormerOpenStream.Close();
ToFileOpenStream.Close();
if (MessageBox.Show("copy finished") == DialogResult.OK)
{
progressBar1.Value = ;
txtOriginalFile.Clear();
txtCopyFile.Clear();
str = "";
}
}
#endregion public delegate void CopyFile_Delegate(); //定义委托/托管线程
/// <summary>
/// 在线程上执行委托(设置托管线程函数)
/// </summary>
public void SetCopyFile()
{
//this.Invoke(new CopyFile_Delegate(RunCopyFile)); //对指定的线程进行托管
//下面两行代码等同上面一行代码
CopyFile_Delegate copyfile_delegate = new CopyFile_Delegate(RunCopyFile); //创建delegate对象
this.Invoke(copyfile_delegate); //调用delegate
} /// <summary>
/// 设置线程,运行copy文件,它与代理CopyFile_Delegate应具有相同的参数和返回类型
/// </summary>
public void RunCopyFile()
{
CopyFile(txtOriginalFile.Text, txtCopyFile.Text + "\\" + str, , progressBar1); //复制文件
Thread.Sleep(); //避免假死
thdCopyFile.Abort(); //关闭线程
} private void btnOriginalFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //打开文件对话框
{
txtOriginalFile.Text = openFileDialog1.FileName; //获取源文件的路径
}
}
private void btnCopyFile_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtCopyFile.Text = folderBrowserDialog1.SelectedPath;//获取目的文件的路径
}
} private void btnBeginToCopy_Click(object sender, EventArgs e)
{ if (txtOriginalFile.Text.Trim() == string.Empty)
{
MessageBox.Show("OriginalFile cannot be empty!");
return; }
else
{
str = txtOriginalFile.Text;//记录源文件的路径
str = str.Substring(str.LastIndexOf('\\') + , str.Length - str.LastIndexOf('\\') - ); //获取源文件的名称
} if (txtCopyFile.Text.Trim() == string.Empty)
{
MessageBox.Show("The Copyfile path cannot be empty!");
return;
}
else
{
thdCopyFile = new Thread(new ThreadStart(SetCopyFile));
thdCopyFile.Start();
} } /// <summary>
/// 给textbox增加tooltip
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtOriginalFile_MouseHover(object sender, EventArgs e)
{
ToolTip tooltip = new ToolTip();
if (txtOriginalFile.Text.Trim() != string.Empty)
{ tooltip.Show(txtOriginalFile.Text, txtOriginalFile);
}
else
{
tooltip.Hide(txtOriginalFile);
} }
/// <summary>
/// 给textbox增加tooltip
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtCopyFile_MouseHover(object sender, EventArgs e)
{
ToolTip tooltip = new ToolTip();
if (txtCopyFile.Text.Trim() != string.Empty)
{ tooltip.Show(txtCopyFile.Text, txtCopyFile);
}
else
{
tooltip.Hide(txtCopyFile);
}
}
}
}

复制文件显示进度条

c#根据文件大小显示文件复制进度条实例的更多相关文章

  1. Flex4/Flash多文件上传(带进度条)实例分享

    要求 必备知识 本文要求基本了解 Adobe Flex编程知识和JAVA基础知识. 开发环境 MyEclipse10/Flash Builder4.6/Flash Player11及以上 演示地址 演 ...

  2. libcurl开源库在Win32程序中使用下载文件显示进度条实例

    一.配置工程引用libcurl库 #define CURL_STATICLIB #include "curl/curl.h" #ifdef _DEBUG #pragma comme ...

  3. PHP_APC扩展dll上传大文件及进度条实例

    1.弄好了APC之后,就是使用它了,下面是个例子,是一个进度条上传的例子,作为笔记记录下来 在这个例子之前,我们需要做如下的设置,如果我们需要上传的是大文件的话,请在您的php.ini文件中做如下的设 ...

  4. html + css + jquery实现简单的进度条实例

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...

  5. Ajax+PHP实现的进度条--实例

    之前重点学习PHP,所以javascript.Ajax都比较弱一点.现在也开始补课了,今天实现了一个进度条的例子,感觉Ajax实现动态页面真的很厉害,并没有想象中的那么难理解. 进度条作为反应实时传输 ...

  6. struts2:上传多个文件时实现带进度条、进度详细信息的示范

    上一篇文章讲了上传单个文件与上传多个文件(属性驱动)的例子.本例是上传多个文件(属性驱动),并且显示进度条.进度详细信息的示范. 在文件上传选择界面,允许用户增加.删除选择的文件,且只能上传指定类型的 ...

  7. iOS:进度条控件的详细使用

    进度条控件:UIProcessView:UIView   功能:顾名思义,用来显示下载进度或者传输数据进度.   属性: @property(nonatomic) UIProgressViewStyl ...

  8. html5中的progress兼容ie,制作进度条样式

    html5新增的progress标签用处很大,它可以制作进度条,不用像以前那样用css来制作进度条! 一.progress使用方法 progress标签很好使用,他有两个属性,value和max,va ...

  9. python预课02 time模块,文本进度条示例,数字类型操作,字符串操作

    time模块 概述:time库是Python中处理时间的标准库,包含以下三类函数 时间获取: time(), ctime(), gmtime() 时间格式化: strftime(), strptime ...

随机推荐

  1. 在浏览器中使用jquery取得iframe中页面中指定元素的值的不同

    自己使用aspx页面中嵌套了ascx的页面其中使用了iframe的一些内容,出现了同一个页面的两种取值的方式 1. 在iframe的包含页面,需要使用iframe的页面中的元素,是需要使用$(wind ...

  2. 教程-FastReport 的安装 心得

    由于要使用报表,所以下载了FastReport 4.7.91,由于是第一次安装和使用FastReport报表,所以在安装的时候走了点弯路.把心得写一下吧. 我是第安装第二遍才完全理解安装过程,也可以定 ...

  3. 有关gcc的扩展__attribute__((unused))

    ================================ Author: taoyuetao Email: tao_yuetao@yahoo.com.cn Blog: taoyuetao.cu ...

  4. light oj 1148 - Mad Counting

    1148 - Mad Counting   PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory Limit: 32 MB M ...

  5. [一]初识SpringMVC

    是什么? web开发框架 为什么用? 功能强大 怎么做? 1.导入jar包 2.配置web.xml <?xml version="1.0" encoding="UT ...

  6. highcharts 柱形图 饼图 加URL或Click事件

    我们在做图表的时候,有时候需要在单个数据上加链接或点击事件,是在plotOptions里的events里设置的 plotOptions: { pie: { cursor: 'pointer', eve ...

  7. Linq to SQL 简单增删改查

    用Linq大大减少了对数据库的一般操作所需的编码量.运行下面事例之前,首先建一个叫做Alien的数据库表. CREATE TABLE [dbo].[Aliens](    [Id] [int] IDE ...

  8. CardsTube/YouTubePlaylist

    CardsTube https://github.com/DesarrolloAntonio/CardsTube YouTubePlaylist https://github.com/akoscz/Y ...

  9. Android 颜色渲染(六) RadialGradient 环形渲染

    Android 颜色处理(六) RadialGradient 环形渲染 public RadialGradient(float x, float y, float radius, int[] colo ...

  10. MAC OS X 终端命令入门 (简单常用整理)

    在这里记下..防止丢失 pwd 当前工作目录 cd(不加参数) 进root cd(folder) 进入文件夹 cd .. 上级目录 cd ~ 返回root cd - 返回上一个访问的目录 rm 文件名 ...