1.窗口界面
主要是文本框textBox、按钮button、进度条prograssBar三大组件所组成的。
2.完整代码
using System;
using System.IO;
using System.Windows.Forms;
using System.Threading;//线程序的命名空间
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace 复制文件显示进度
{
public partial class FileCopyPlan : Form
{
public FileCopyPlan()
{
InitializeComponent();
}
private Thread thdCopyFile; //创建一个线程
private string str = ""; //用来记录源文件的名字
FileStream FormerOpen; //实例化源文件FileStream类
FileStream ToFileOpen; //实例化目标文件FileStream类
public void CopyFile(string FormerFile, string ToFile, int SectSize, ProgressBar progressBar1)
{
progressBar1.Value = 0;//设置进度条的当前位置为0
progressBar1.Minimum = 0; //设置进度条的最小值为0
//创建目的文件,如果已存在将被覆盖
FileStream fileToCreate = new FileStream(ToFile, FileMode.Create);
fileToCreate.Close(); //关闭所有fileToCreate的资源
fileToCreate.Dispose();//释放所有fileToCreate的资源
FormerOpen = new FileStream(FormerFile, FileMode.Open, FileAccess.Read);//以只读方式打开源文件
ToFileOpen = new FileStream(ToFile, FileMode.Append, FileAccess.Write); //以写方式打开目的文件
int max = Convert.ToInt32(Math.Ceiling((double)FormerOpen.Length / (double)SectSize));
progressBar1.Maximum = max;
int FileSize;
if (SectSize <= FormerOpen.Length) //如果分段拷贝,即每次拷贝内容小于文件总长度
{
byte[] buffer = new byte[SectSize]; //根据传输的大小,定义一个字节数组,用来存储传输的字节
int copied = 0;//记录传输的大小
int tem_n = 1; //设置进度栏中进度的增加个数
while (copied <= ((int)FormerOpen.Length - SectSize))
{
FileSize = FormerOpen.Read(buffer, 0, SectSize);//从0开始读到buffer字节数组中,每次最大读SectSize
FormerOpen.Flush(); //清空缓存
ToFileOpen.Write(buffer, 0, SectSize); //向目的文件写入字节
ToFileOpen.Flush(); //清空缓存
ToFileOpen.Position = FormerOpen.Position; //是源文件的目的文件流的位置相同
copied += FileSize; //记录已经拷贝的大小
progressBar1.Value = progressBar1.Value + tem_n; //增加进度栏的进度块
}
int leftSize = (int)FormerOpen.Length - copied; //获取剩余文件的大小
FileSize = FormerOpen.Read(buffer, 0, leftSize);//读取剩余的字节
FormerOpen.Flush();
ToFileOpen.Write(buffer, 0, leftSize); //写入剩余的部分
ToFileOpen.Flush();
}
else //如果整体拷贝,即每次拷贝内容大于文件总长度
{
byte[] buffer = new byte[FormerOpen.Length]; //获取文件大小
FormerOpen.Read(buffer, 0, (int)FormerOpen.Length); //获取源文件大小
FormerOpen.Flush(); //清空缓存
ToFileOpen.Write(buffer, 0, (int)FormerOpen.Length);//写入字节
ToFileOpen.Flush(); //清空缓存
}
FormerOpen.Close();//释放所有资源
ToFileOpen.Close();//释放所有资源
if (MessageBox.Show("复制完成") == DialogResult.OK)
{
progressBar1.Value = 0;//设置进度条当前位置为0
textBox1.Clear();//清空文本
textBox2.Clear();//清空文本
str = "";
}
}
public delegate void CopyFile_Delegate(); //定义委托/托管线程
public void SetCopyFile()
{
//this.Invoke(new CopyFile_Delegate(RunCopyFile)); //对指定的线程进行托管
//下面两行代码等同上面一行代码
CopyFile_Delegate copyfile_delegate = new CopyFile_Delegate(RunCopyFile); //创建delegate对象
this.Invoke(copyfile_delegate); //调用delegate
}
public void RunCopyFile()
{
CopyFile(textBox1.Text, textBox2.Text + "\\" + str, 1024, progressBar1); //复制文件
Thread.Sleep(0); //避免假死
thdCopyFile.Abort(); //关闭线程
}
private void button1_Click(object sender, EventArgs e)//“源文件”按钮
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //打开文件对话框
{
textBox1.Text = openFileDialog1.FileName; //获取源文件的路径
}
}
private void button2_Click(object sender, EventArgs e)//“目的文件”按钮
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
textBox2.Text = folderBrowserDialog1.SelectedPath;//获取目的文件的路径
}
}
private void botton3_Click(object sender, EventArgs e)//“复制”按钮
{
if (textBox1.Text.Trim() == string.Empty)
{
MessageBox.Show("OriginalFile cannot be empty!");
return;
}
else
{
str = textBox1.Text;//记录源文件的路径
str = str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1); //获取源文件的名称
}
if (textBox2.Text.Trim() == string.Empty)
{
MessageBox.Show("The Copyfile path cannot be empty!");
return;
}
else
{
thdCopyFile = new Thread(new ThreadStart(SetCopyFile));
thdCopyFile.Start();
}
}
private void textBox1_MouseHover(object sender, EventArgs e)//源文件文本框事件
{
ToolTip tooltip = new ToolTip();
if (textBox1.Text.Trim() != string.Empty)
{
tooltip.Show(textBox1.Text, textBox1);
}
else
{
tooltip.Hide(textBox1);
}
}
private void textBox2_MouseHover(object sender, EventArgs e)//目的文件文本框事件
{
ToolTip tooltip = new ToolTip();
if (textBox2.Text.Trim() != string.Empty)
{
tooltip.Show(textBox2.Text, textBox2);
}
else
{
tooltip.Hide(textBox2);
}
}
}
}