ある場所から別の場所にファイルをコピーしているときに進行状況バーを更新する方法を探しています。大きなファイルをコピー中に進行状況バーを更新する
私はBackgroundWorker
でコピーを行い、バックグラウンドでも進行状況バーを更新しています。私はfile.lengthを使用してファイルサイズを取得し、パーセンテージを使用してバーをそのように更新しましたが、喜んでは使用しませんでした。
私はコードを添付しており、何か助けていただければ幸いです。ありがとうございます。
namespace Copier
{パブリック部分クラスをForm1:フォーム {公共のForm1(){ のInitializeComponent(); }
// Declare for use in all methods
public string copyFrom;
public string copyTo;
private void btnCopyFrom_Click(object sender, EventArgs e)
{
// uses a openFileDialog, openFD, to chose the file to copy
copyFrom = "";
openFD.InitialDirectory = @"C:\Documents and Settings\user\My Documents";
openFD.FileName = "";
//openFD.ShowDialog();
if (openFD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("cancel button clicked");
}
else
{
// sets copyFrom = to the file chosen from the openFD
copyFrom = openFD.FileName;
// shows it in a textbox
txtCopyFrom.Text = copyFrom;
}
}
private void btnCopyTo_Click(object sender, EventArgs e)
{
//uses folderBrowserDialog, folderBD, to chose the folder to copy to
copyTo = "";
this.folderBD.RootFolder = System.Environment.SpecialFolder.MyComputer;
this.folderBD.ShowNewFolderButton = false;
//folderBD.ShowDialog();
//DialogResult result = this.folderBD.ShowDialog();
if (folderBD.ShowDialog() == DialogResult.Cancel)
{
MessageBox.Show("cancel button clicked");
}
else
{
// sets copyTo = to the folder chosen from folderBD
copyTo = this.folderBD.SelectedPath;
//shows it in a textbox.
txtCopyTo.Text = copyTo;
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
copyBGW.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
Application.Exit();
}
//=================================================================
// BackGroundWorkers
//=================================================================
private void copyBGW_DoWork(object sender, DoWorkEventArgs e)
{
try
{
// copies file
string destinatationPath = Path.Combine(copyTo, Path.GetFileName(copyFrom));
File.Copy(copyFrom, destinatationPath);
MessageBox.Show("File Copied");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
それとも誰かが私にちょうどそれがフォームが凍結していないことを示しているように、プログレスバーが自己で行くようにする方法を示すことができましたか?
は、これまで
http://stackoverflow.com/questions/187768/can-i-show-file-copy-progress-using-fileinfo-copyto -in-net –
補足として、この[チュートリアル](http://www.albahari.com/threading/part3.aspx#_BackgroundWorker)をBackgroundWorkerスレッドで読むことをお勧めします。BackgroundWorkerスレッドの 'RunWorkerCompleted'イベントに' MessageBox.Show'や例外処理のようなコードを置きます。 –