Find and Replace text in all files of a given folder , including subfolders with C#
If you want to find and replace some text in all files of a given folder, including subfolders, you can use the following C# code:
string rootfolder = @"C:\Temp"; string[] files = Directory.GetFiles(rootfolder, "*.*", SearchOption.AllDirectories); foreach (string file in files) { try { string contents = File.ReadAllText(file); contents = contents.Replace(@"Text to find", @"Replacement text"); // Make files writable File.SetAttributes(file, FileAttributes.Normal); File.WriteAllText(file, contents); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
Find and Replace text in all files of a given folder , including subfolders with C#
Reviewed by Bhaumik Patel
on
12:55 AM
Rating: