■ 使用している主なコントロール
* TextBox : 3、Button : 1
* Label : 1
* BackgroundWorker/ProgressBar : 各1
* CheckBox : 2 (オプション・必須じゃない)
■ サンプル
// ファイル名のフォーマット [FileName][FileNo].[Extention]
private readonly string FileNoFormat = "{0}{1:D3}{2}";
// リネームするファイル名
private string renameFile = string.Empty;
/// <summary>
/// スタートボタン押下後の動作
/// </summary>
private void buttonStart_Click(object sender, EventArgs e)
{
if (Directory.Exists(this.textBoxTarget.Text))
{
this.toolStripStatusLabel.Text = "Start!";
// ファイル名リネーム処理
this.ChangeFileNames(
new DirectoryInfo(this.textBoxTarget.Text));
}
else
{
this.toolStripStatusLabel.Text = "No Exist!";
}
}
/// <summary>
/// ファイル名リネーム処理
/// </summary>
private void ChangeFileNames(DirectoryInfo directoryInfo)
{
string outputFolderFullPath = this.textBoxOutputFolder.Text;
if (string.IsNullOrWhiteSpace(outputFolderFullPath))
{
outputFolderFullPath = this.textBoxTarget.Text;
}
else
{
if (!Directory.Exists(outputFolderFullPath))
{
// なかったらフォルダを作成
Directory.CreateDirectory(outputFolderFullPath);
}
}
this.renameFile = this.textBoxRenameFile.Text;
this.renameFile = Path.Combine(outputFolderFullPath, this.renameFile);
// 時間が掛かるのでスレッド処理
this.backgroundWorker.RunWorkerAsync(directoryInfo);
}
/// <summary>
/// バックグラウンドワーカーの処理
/// </summary>
/// <remarks>時間のかかる処理を行うメソッドを実装する</remarks>
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
// 待機状態
Cursor.Current = Cursors.WaitCursor;
DirectoryInfo directoryInfo = e.Argument as DirectoryInfo;
int totalfileNo = directoryInfo.GetFiles().Count();
int fileNo = 1;
// すべてのファイルの読み取り専用属性を解除する
foreach (System.IO.FileInfo fileInfo in directoryInfo.GetFiles())
{
// キャンセルされてないか定期的にチェック
if (this.backgroundWorker.CancellationPending)
{
e.Cancel = true;
return;
}
// 変更したいファイル名
string renameFileName = string.Format(
this.FileNoFormat, this.renameFile , fileNo, fileInfo.Extension);
if ((fileInfo.Attributes & System.IO.FileAttributes.ReadOnly)
== System.IO.FileAttributes.ReadOnly)
{
fileInfo.Attributes = System.IO.FileAttributes.Normal;
}
// ファイルをコピーする
fileInfo.CopyTo(renameFileName
, this.checkBoxOverwriteFlg.Checked);
if (this.checkBoxDeleteFlg.Checked)
{
fileInfo.Delete();
}
// 進捗率を算出
int percentage = fileNo * 100 / totalfileNo;
// 算出した進捗率をプログレスバーに反映させるために
// ProgressChangedイベントを発生させる
this.backgroundWorker.ReportProgress(percentage);
fileNo++;
}
}
catch (Exception ex)
{
// 例外処理
this.toolStripStatusLabel.Text = "Error!!" + ex.Message;
}
finally
{
// このメソッドからの戻り値
e.Result = "完了しました";
// 元に戻す
Cursor.Current = Cursors.Default;
}
}
}
/// <summary>
/// バックグラウンドワーカーの終了処理
/// </summary>
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs
{
if (e.Cancelled)
{
this.toolStripStatusLabel.Text = "キャンセルされました";
MessageBox.Show("キャンセルされました");
}
else
{
// 処理結果の表示
this.toolStripStatusLabel.Text = e.Result.ToString();
if(this.checkBoxOverwriteFlg.Checked
&& !string.IsNullOrWhiteSpace(
this.textBoxOutputFolder.Text))
{
// 元フォルダを削除
Directory.Delete(this.textBoxTarget.Text);
}
MessageBox.Show("完了");
}
// コントロールを元に戻す
this.toolStripProgressBar.Value = 0;
this.toolStripProgressBar.Enabled = false;
}
/// <summary>
/// バックグラウンドワーカーの終了処理
/// </summary>
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// 進捗状況をラベルに表示
this.toolStripStatusLabel.Text = e.ProgressPercentage + "%実行完了";
// プログレスバーに進捗率を反映
this.toolStripProgressBar.Value = e.ProgressPercentage;
}
/// <summary>
/// 進行状況表示フォームキャンセル押下処理
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>
/// ここでは、プログレスバーをクリックするとキャンセルするように実装している
/// </remarks>
private void toolStripProgressBar_Click(object sender, EventArgs e)
{
// 時間のかかる処理のキャンセル
this.backgroundWorker.CancelAsync();
}