我可以在不使用 foreach 循环的情况下优化代码吗?目标是找到具有 .config 扩展名的文件并在将其部署到生产之前替换指定的文本。
DirectoryInfo di= new DirectoryInfo(@"C:\Inetpub\Wwwroot\workframew\Presentation\Website");
FileInfo[] fl= di.GetFiles("*.config");
foreach (FileInfo fi in fl)
{
//File.Replace(fi.FullName,fi.FullName,.ReadAllText(fi.FullName) = File.ReadAllText(fi.FullName).Replace(@"F:\LogFiles\ApplicationLogs\Compass\",@"C:\Compass_350\Compass\");
string filePath = fi.FullName;
StreamReader reader = new StreamReader(filePath);
string content = reader.ReadToEnd();
reader.Close();
content = Regex.Replace(content, @"F:\LogFiles\ApplicationLogs\Compass\", @"C:\Compass_350\Compass\");
StreamWriter writer = new StreamWriter(filePath);
writer.Write(content);
writer.Close();
}