コード例 #1
0
        private void btnReplace_Click(object sender, EventArgs e)
        {
            if (_iFiles == -1 ||
                _iFiles >= _files.Length)
            {
                MyMessage.ShowError("Ther is no current file.");
            }
            else
            {
                if (!chkFileDirectoriesNames.Checked)
                {
                    Encoding encoding = DetectEncoding.GetEncoding(_files[_iFiles]);
                    File.WriteAllText(_files[_iFiles], txtPreview.Text.Replace(txtFind.Text, txtReplace.Text), encoding);
                }
                else
                {
                    FileInfo auxFile = new FileInfo(_files[_iFiles]);
                    auxFile.MoveTo($"{auxFile.DirectoryName}\\{auxFile.Name.Replace(txtFind.Text, txtReplace.Text)}");
                }

                this.btnFindNext_Click(sender, e);
            }
        }
コード例 #2
0
        private void btnReplaceAll_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtFind.Text))
            {
                MyMessage.ShowError("Find text cannot be empty.");
                txtFind.Focus();
            }
            else
            {
                startThreadSearch(() =>
                {
                    try
                    {
                        if (_iFiles == -1 ||
                            _iFiles >= _files.Length)
                        {
                            _iFiles = -1;
                            _files  = GetFiles();
                        }

                        if (_files.Length == 0)
                        {
                            MyMessage.ShowError("No files found.");
                        }
                        else
                        {
                            RegexOptions options = !chkCaseSensitive.Checked ? RegexOptions.IgnoreCase : RegexOptions.None;
                            int countMatches     = 0;
                            string evaluator(Match m)
                            {
                                countMatches++;
                                return(txtReplace.Text);
                            }

                            if (!chkFileDirectoriesNames.Checked)
                            {
                                for (_iFiles++; _iFiles < _files.Length; _iFiles++)
                                {
                                    string fileContent = File.ReadAllText(_files[_iFiles]);

                                    WriteLog($"Replacing {_iFiles + 1} of {_files.Length} - {_files[_iFiles]}");

                                    countMatches           = 0;
                                    string replacedContent = Regex.Replace(fileContent, txtFind.Text, evaluator, options);
                                    WriteLog($"{countMatches} incidences");

                                    Encoding encoding = DetectEncoding.GetEncoding(_files[_iFiles]);
                                    File.WriteAllText(_files[_iFiles], replacedContent, encoding);
                                }
                            }
                            else
                            {
                                for (_iFiles++; _iFiles < _files.Length; _iFiles++)
                                {
                                    WriteLog($"Replacing {_iFiles + 1} of {_files.Length} - {_files[_iFiles]}");

                                    FileInfo file = new FileInfo(_files[_iFiles]);

                                    string newFileName = Regex.Replace(file.Name, txtFind.Text, evaluator, options);

                                    file.MoveTo(Path.Combine(file.DirectoryName, newFileName));
                                }
                            }
                        }
                    }
                    catch (ThreadAbortException) { }
                    catch (Exception ex)
                    {
                        MyMessage.ShowError(ex.Message);
                    }
                });
            }
        }