初學(xué)者,照著書上的抄襲制作,但已經(jīng)理解了里面的意思和應(yīng)用,并且進(jìn)行了稍微改善和異常捕捉。這里記錄下,以防以后用到這方面的知識(shí)點(diǎn)。
窗體設(shè)計(jì):
code:
namespace CopyProgress015
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Thread thdCopyFile; //創(chuàng)建一個(gè)線程
private string str = ""; //用來(lái)記錄源文件的名字
FileStream FormerOpenStream; //實(shí)例化源文件FileStream類
FileStream ToFileOpenStream; //實(shí)例化目標(biāo)文件FileStream類
#region //復(fù)制文件 函數(shù)
/// <summary>
/// 復(fù)制文件
/// </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;//設(shè)置進(jìn)度條的當(dāng)前位置為0
progressBar1.Minimum = 0; //設(shè)置進(jìn)度條的最小值為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); //創(chuàng)建目的文件,如果已存在將被覆蓋
fileToCreate.Close();//關(guān)閉所有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));//根據(jù)一次傳輸?shù)拇笮?,?jì)算最大傳輸個(gè)數(shù). Math.Ceiling 方法 (Double),返回大于或等于指定的雙精度浮點(diǎn)數(shù)的最小整數(shù)值。
progressBar1.Maximum = max;//設(shè)置進(jìn)度條的最大值
int FileSize; //每次要拷貝的文件的大小
if (TranSize < FormerOpenStream.Length) //如果分段拷貝,即每次拷貝內(nèi)容小于文件總長(zhǎng)度
{
byte[] buffer = new byte[TranSize]; //根據(jù)傳輸?shù)拇笮?,定義一個(gè)字節(jié)數(shù)組,用來(lái)存儲(chǔ)傳輸?shù)淖止?jié)
int copied = 0;//記錄傳輸?shù)拇笮?br> int tem_n = 1;//設(shè)置進(jìn)度欄中進(jìn)度的增加個(gè)數(shù)
while (copied <= ((int)FormerOpenStream.Length - TranSize))
{
FileSize = FormerOpenStream.Read(buffer, 0, TranSize);//從0開始讀到buffer字節(jié)數(shù)組中,每次最大讀TranSize
FormerOpenStream.Flush(); //清空緩存
ToFileOpenStream.Write(buffer, 0, TranSize); //向目的文件寫入字節(jié)
ToFileOpenStream.Flush();//清空緩存
ToFileOpenStream.Position = FormerOpenStream.Position; //是源文件的目的文件流的位置相同
copied += FileSize; //記錄已經(jīng)拷貝的大小
progressBar1.Value = progressBar1.Value + tem_n; //增加進(jìn)度欄的進(jìn)度塊
}
int leftSize = (int)FormerOpenStream.Length - copied; //獲取剩余文件的大小
FileSize = FormerOpenStream.Read(buffer, 0, leftSize); //讀取剩余的字節(jié)
FormerOpenStream.Flush();
ToFileOpenStream.Write(buffer, 0, leftSize); //寫入剩余的部分
ToFileOpenStream.Flush();
}
else //如果整體拷貝,即每次拷貝內(nèi)容大于文件總長(zhǎng)度
{
byte[] buffer = new byte[FormerOpenStream.Length];
FormerOpenStream.Read(buffer, 0, (int)FormerOpenStream.Length);
FormerOpenStream.Flush();
ToFileOpenStream.Write(buffer, 0, (int)FormerOpenStream.Length);
ToFileOpenStream.Flush();
}
FormerOpenStream.Close();
ToFileOpenStream.Close();
if (MessageBox.Show("copy finished") == DialogResult.OK)
{
progressBar1.Value = 0;
txtOriginalFile.Clear();
txtCopyFile.Clear();
str = "";
}
}
#endregion
public delegate void CopyFile_Delegate(); //定義委托/托管線程
/// <summary>
/// 在線程上執(zhí)行委托(設(shè)置托管線程函數(shù))
/// </summary>
public void SetCopyFile()
{
//this.Invoke(new CopyFile_Delegate(RunCopyFile)); //對(duì)指定的線程進(jìn)行托管
//下面兩行代碼等同上面一行代碼
CopyFile_Delegate copyfile_delegate = new CopyFile_Delegate(RunCopyFile); //創(chuàng)建delegate對(duì)象
this.Invoke(copyfile_delegate); //調(diào)用delegate
}
/// <summary>
/// 設(shè)置線程,運(yùn)行copy文件,它與代理CopyFile_Delegate應(yīng)具有相同的參數(shù)和返回類型
/// </summary>
public void RunCopyFile()
{
CopyFile(txtOriginalFile.Text, txtCopyFile.Text + "\\" + str, 1024, progressBar1); //復(fù)制文件
Thread.Sleep(0); //避免假死
thdCopyFile.Abort(); //關(guān)閉線程
}
private void btnOriginalFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK) //打開文件對(duì)話框
{
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('\\') + 1, str.Length - str.LastIndexOf('\\') - 1); //獲取源文件的名稱
}
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);
}
}
}
}
聯(lián)系客服