using System.Text; using System.IO; namespace ConsoleApp1 { class Program { static void Main(string[] args) { CommonCSFile(); } //注释掉所有*.cs文件 private static void CommonCSFile() { //项目代码所在目录 string src = @"D:\Scripts"; StringBuilder sb = new StringBuilder(); DirectoryInfo Dir = new DirectoryInfo(src); //递归遍历所有目录 foreach (FileInfo f in Dir.GetFiles("*.cs", SearchOption.AllDirectories)) //查找所有文件 { var fr = f.OpenText(); string content = fr.ReadToEnd(); sb.AppendLine("#if false"); sb.AppendLine(content); sb.AppendLine("#endif"); fr.Close(); var fw = f.CreateText(); fw.Write(sb.ToString()); fw.Flush(); fw.Close(); sb.Clear(); } } } }