c\#파일 크기 에 따라 파일 복사 진행 바 인 스 턴 스 를 표시 합 니 다.

초보 자 는 책의 표절 에 따라 만 들 었 으 나 그 뜻 과 응용 을 이 해 했 고 약간 개선 되 고 이상 한 포착 을 했다.앞으로 이 분야 의 지식 을 사용 하지 않도록 기록 해 두 세 요.
창 디자인:

code:

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;// 0
 progressBar1.Minimum = 0; // 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 = 0;//
     int tem_n = 1;//
     while (copied <= ((int)FormerOpenStream.Length - TranSize))
     {
  FileSize = FormerOpenStream.Read(buffer, 0, TranSize);// 0 buffer , TranSize
  FormerOpenStream.Flush();   //
  ToFileOpenStream.Write(buffer, 0, 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, 0, leftSize); //
     FormerOpenStream.Flush();
     ToFileOpenStream.Write(buffer, 0, leftSize); //
     ToFileOpenStream.Flush();
 }
 else // ,
 {
     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>
    /// ( )
    /// </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, 1024, progressBar1); //
 Thread.Sleep(0); //
 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('\\') + 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);
 }

    }

}
}

좋은 웹페이지 즐겨찾기