最近nfx462的项目升级.NET6需要批量替换Ilogger为Ilogger<类名>
vs自带的搜索替换其正则表达式好像只能匹配一行,直接扫描文件替换吧
// See https://aka.ms/new-console-template for more information Console.WriteLine("Hello, World!"); string path = ""; var files = Directory.GetFiles(path, "*.cs", SearchOption.AllDirectories).ToList(); files = files.Where(x => { string[] arr = x.Split('\\'); var fileName = arr[^1]; return fileName.Count(c => c == '.') == 1 && !fileName.StartsWith('I'); }).ToList(); foreach (string filePath in files) { string[] arr = filePath.Split('\\'); var fileName = arr[^1].Substring(0, arr[^1].Length - 3); bool hasChange = false; if (File.Exists(filePath)) { string[] lines = System.IO.File.ReadAllLines(filePath); for (int i = 0; i < lines.Length; i++) { if (lines[i].Contains("ILogger")) { hasChange = true; lines[i] = lines[i].Replace("ILogger", $"ILogger<{fileName}>"); } } if (hasChange) { File.WriteAllLines(filePath, lines); } } } Console.ReadKey();
标签:files,arr,string,批量,filePath,lines,fileName,关键字,NET From: https://www.cnblogs.com/nene22----/p/17076073.html