C# 文本文件的查找及替换(WinForm)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SupperReplace
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            openFileDialog1.AddExtension = true;
            openFileDialog1.DefaultExt = "txt";
            openFileDialog1.Filter = @"*.txt|*.txt";
        }

        private void btnSelectFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                txtBoxFile.Text = openFileDialog1.FileName;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string search = txtBoxSearch.Text.Trim();
            if (!string.IsNullOrEmpty(search))
            {
                string fileName = txtBoxFile.Text.Trim();
                using (TextReader tr = File.OpenText(fileName))
                {
                    string content = tr.ReadToEnd();
                    int count = CountNumber(content, search);
                    DisplaySearchCount(count);
                }
            }
        }

        void DisplaySearchCount(int count)
        {
            lblSearchResult.Text = "共查找到:\r\n" + count.ToString() + "\r\n个结果。";
        }

        void DisplayReplaceCount(int count)
        {
            DisplaySearchCount(count);
            lblReplaceResult.Text = "共查找到:\r\n" + count.ToString() + "\r\n个结果。";
        }


        private int CountNumber(string father, string son)
        {
            //判断是否有相同字符串
            if (father.Contains(son))
            {
                //用一个字符串储存被替换后的整个字符串
                string strReplaced = father.Replace(son, "");
                //两个字符串的长度差值 / 被替换掉的字符串单位长度
                return (father.Length - strReplaced.Length) / son.Length;
            }
            //没有返回0个
            return 0;
        }

        private void btnReplace_Click(object sender, EventArgs e)
        {
            string search = txtBoxSearch.Text;
            string replace = txtBoxReplace.Text;
            if (!string.IsNullOrEmpty(search))
            {
                string fileName = txtBoxFile.Text.Trim();
                string content, result;
                using (TextReader tr = File.OpenText(fileName))
                {
                    content = tr.ReadToEnd();
                    result = content.Replace(search, replace);

                }
                //写入文件
                using (FileStream fs = File.Open(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    Byte[] info = new UTF8Encoding(true).GetBytes(result);
                    fs.Write(info, 0, info.Length);
                }
                int count = CountNumber(content, search);
                DisplayReplaceCount(count);
            }
        }
    }
}
 

猜你喜欢

转载自blog.csdn.net/johnsuna/article/details/124332634