批量替换文本的工具

1.功能:给出一个文件夹,能够替这个文件夹下所有.cs文件中指定的字符串为新的字符串(文件类型当然可以改了)

2.Form1.cs代码

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

namespace ReplaceText
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtPath.Text==""||txtTextToFind.Text=="")
            {
                MessageBox.Show("路径和要查找的文本都是必须的!!");
                return;
            }

            string strPath = txtPath.Text;
            if (Directory.Exists(strPath))
            {
                FileInfo[] files = GetFileList(strPath);

                foreach (FileInfo item in files)
                {
                    
                    ReplaceText(item.FullName,txtTextToFind.Text,txtTextNew.Text);
                }


            }

        }



        //获取文件信息
        private FileInfo[] GetFileList(string path)
        {


            DirectoryInfo dir = new DirectoryInfo(path);


            return dir.GetFiles("*.cs"); 
        }

        //替换文件指定文本并保存
        private void ReplaceText(string fileName,string strOld,string strNew)
        {
            string[] str = File.ReadAllLines(@"" + fileName, Encoding.Default);//将文件按行读出,保存在字符串数组中
            if (str.Length > 0) //判断文件是否为空
            {
                int count = 0;
                foreach (string item in str)
                {
                    if (item.Contains(strOld))
                    {
                        str[count] = item.Replace(strOld, strNew);
                    }
                    count++;
                }
            }

            
            string path=fileName.Substring(0,fileName.LastIndexOf('\'))+"\NewBuild\";

            string name = fileName.Substring(fileName.LastIndexOf('\')+1);
            //创建文件夹
            CreateFolder(path);


            File.WriteAllLines(path+name, str);//将替换后的数组按行重新写入文件中,将覆盖原内容
        
        }

        //创建文件夹
        private void CreateFolder(string path)
        {
            if(!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            
            }
        
        }

        
    }
}

3.Form1.designer.cs

namespace ReplaceText
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.txtPath = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtTextToFind = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.txtTextNew = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // txtPath
            // 
            this.txtPath.Location = new System.Drawing.Point(59, 20);
            this.txtPath.Name = "txtPath";
            this.txtPath.Size = new System.Drawing.Size(396, 21);
            this.txtPath.TabIndex = 0;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(24, 23);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(29, 12);
            this.label1.TabIndex = 1;
            this.label1.Text = "路径";
            // 
            // txtTextToFind
            // 
            this.txtTextToFind.Location = new System.Drawing.Point(95, 66);
            this.txtTextToFind.Name = "txtTextToFind";
            this.txtTextToFind.Size = new System.Drawing.Size(258, 21);
            this.txtTextToFind.TabIndex = 0;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(24, 69);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(65, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "查找的文本";
            // 
            // txtTextNew
            // 
            this.txtTextNew.Location = new System.Drawing.Point(95, 96);
            this.txtTextNew.Name = "txtTextNew";
            this.txtTextNew.Size = new System.Drawing.Size(258, 21);
            this.txtTextNew.TabIndex = 0;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(24, 99);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(41, 12);
            this.label3.TabIndex = 1;
            this.label3.Text = "替换成";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(359, 66);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(141, 56);
            this.button1.TabIndex = 2;
            this.button1.Text = "开始生成";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(461, 23);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(72, 16);
            this.checkBox1.TabIndex = 3;
            this.checkBox1.Text = "子文件夹";
            this.checkBox1.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(535, 157);
            this.Controls.Add(this.checkBox1);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.txtTextNew);
            this.Controls.Add(this.txtTextToFind);
            this.Controls.Add(this.txtPath);
            this.Name = "Form1";
            this.Text = "批量替换";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txtPath;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txtTextToFind;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.TextBox txtTextNew;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.CheckBox checkBox1;
    }
}

原文地址:https://www.cnblogs.com/hbhzz/p/3244062.html