示例#1
0
        void Find(object sender, EventArgs e)
        {
            azukiAfter.UpdateNewlineChars();
            azukiBefore.UpdateNewlineChars();

            if (!Directory.Exists(DirPath))
            {
                MessageBox.Show("存在しないフォルダです。", "エラー",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (string.IsNullOrEmpty(Pattern))
            {
                MessageBox.Show("フィルタが入力されていません。", "エラー",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (InformBeforeTextEmpty && string.IsNullOrEmpty(BeforeText))
            {
                string msg = "置換前のテキストが入力されていません。検索を実行しますか?\n\n" +
                             string.Format("場所: {0}\nサブフォルダ: {1}\nフィルタ: {2}\n文字コード: {3}\n正規表現: {4}",
                                           tbDirPath.Text, cbSubDir.Checked ? "含める" : "含めない",
                                           tbPattern.Text, comboBoxEnc.SelectedItem.ToString(),
                                           cbRegex.Checked ? "適用する" : "適用しない");
                DialogResult dr = MessageBox.Show(
                    msg, "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
                if (dr != DialogResult.Yes)
                {
                    return;
                }
            }
            else if (InformBeforeSearch) // 'else if' to avoid inquiring twice in succession
            {
                string msg = "検索を開始します。よろしいですか?\n\n" +
                             string.Format("場所: {0}\nサブフォルダ: {1}\nフィルタ: {2}\n文字コード: {3}\n正規表現: {4}",
                                           tbDirPath.Text, cbSubDir.Checked ? "含め" : "含めない",
                                           tbPattern.Text, comboBoxEnc.SelectedItem.ToString(),
                                           cbRegex.Checked ? "適用する" : "適用しない");
                DialogResult dr = MessageBox.Show(
                    msg, "確認", MessageBoxButtons.YesNo, MessageBoxIcon.None);
                if (dr != DialogResult.Yes)
                {
                    return;
                }
            }

            fileListView.AzukiBefore  = azukiBefore;
            fileListView.AzukiAfter   = azukiAfter;
            fileListView.RegexEnabled = RegexEnabled;
            fileListView.RegexOptions = RegexMultiline ? RegexOptions.Multiline : RegexOptions.Singleline;  // do not use |=

            _dirPath        = DirPath;
            _pattern        = Pattern;
            _beforeText     = BeforeText;
            _inclSubDir     = InclSubDir;
            _inclHiddenDir  = InclHiddenDir;
            _inclHiddenFile = InclHiddenFile;
            _regEx          = RegexEnabled;
            _encNumber      = EncNumber;
            _regExMultiline = RegexMultiline;

            btnFind.Enabled = false;
            fileListView.Items.Clear();

            Progress.Clear();

            using (BackgroundWorker bw = new BackgroundWorker())
            {
                bw.WorkerReportsProgress      = true;
                bw.WorkerSupportsCancellation = true;
                bw.DoWork += new DoWorkEventHandler(DoFindBkgnd);

                progressDialog      = new ProgressDialog(bw, Progress);
                progressDialog.Text = "検索中...";

                bw.RunWorkerAsync();
            }

            ProgressDialogResult result = progressDialog.ShowDialog();

            if (result == ProgressDialogResult.Success)
            {
                fileListView.Items.Clear();
                foreach (ListViewItem item in _fileListView.Items)
                {
                    fileListView.Items.Add(item.Clone() as ListViewItem);
                }
                fileListView.ListValid = _fileListView.ListValid;
            }

            btnFind.Enabled = true;

            if (btnReplace.Enabled)
            {
                btnReplace.Focus();
            }
            else
            {
                btnFind.Focus();
            }

            if (Progress.Canceled)
            {
                Progress.ErrorDescription = "処理が中断されました。";
                ErrorDialog dialog = new ErrorDialog(Progress);
                dialog.ShowDialog();
                fileListView.ListValid = false;
            }
            else
            {
                if (Progress.ErrorOccured)
                {
                    Progress.ErrorDescription = "アクセスできないファイルまたはフォルダがありました。" + Environment.NewLine + "または、その他のエラーが発生しました。";
                    ErrorDialog dialog = new ErrorDialog(Progress);
                    dialog.ShowDialog();

                    if (fileListView.Items.Count > 0 &&
                        !string.IsNullOrEmpty(fileListView.Items[0].SubItems[1].Text))
                    {
                        fileListView.ListValid = true;
                    }
                    else
                    {
                        fileListView.ListValid = false;
                    }
                }
                if (InformAfterSearch)
                {
                    int    fileNum = fileListView.Items[0].Checked ? fileListView.Items.Count : 0;
                    string msg     = "検索が完了しました。\n\n" +
                                     string.Format("見つかったファイル: {0} 件", fileNum);
                    MessageBox.Show(msg, "確認", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }