How can I write code that changes the text in all .txt files located in the same directory as the application? That is, if I threw the software into the folder, then after launching the software, the software changes the text in the folder where I threw it.
|
1 answer
So keep the program for you, I think you will continue to implement the requirements yourself.
using System; using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; namespace TextRoot.ConsoleApplication { internal class Program { private static void Main() { var finder = new TextFileFinder(); var editor = new FileEditor(finder); //var inputFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "question.txt"); //editor.EditOne(inputFile, textLine => Regex.Replace(textLine, "(.*):", "P$@*#!")); var inputDirectory = AppDomain.CurrentDomain.BaseDirectory; editor.EditAll(inputDirectory, textLine => Regex.Replace(textLine, "(.*):", "P$@*#!")); Console.ReadKey(); } } public class FileEditor { private readonly TextFileFinder _finder; private readonly List<string> _editedTextLines; public FileEditor(TextFileFinder finder) { _finder = finder; _editedTextLines = new List<string>(); } public void EditOne(string filePath, Func<string, string> change) { try { MakeChanges(filePath, change); SaveChanges(filePath); Console.WriteLine($"Text in '{filePath}' was changed."); } catch (Exception ex) { Console.WriteLine(ex.Message); } } public void EditAll(string directoryPath, Func<string, string> change) { try { foreach (var file in _finder.FindAll(directoryPath)) EditOne(file.FullName, change); } catch (Exception ex) { Console.WriteLine(ex.Message); } } #region Private Helpers private void MakeChanges(string filePath, Func<string, string> change) { using (var file = _finder.Find(filePath).OpenRead()) using (var reader = new StreamReader(file)) { while (!reader.EndOfStream) { var textLine = reader.ReadLine(); _editedTextLines.Add(change(textLine)); } } } private void SaveChanges(string filePath) { using (var file = _finder.Find(filePath).Open(FileMode.Truncate)) using (var writer = new StreamWriter(file)) { foreach (var line in _editedTextLines) writer.WriteLine(line); _editedTextLines.Clear(); } } #endregion } public class TextFileFinder { public FileInfo Find(string filePath) { var fileExtension = Path.GetExtension(filePath); if (fileExtension != ".txt") throw new NotSupportedException($"File extension '{fileExtension}' is not supported."); var fileInfo = new FileInfo(filePath); if (!fileInfo.Exists) throw new FileNotFoundException(); return fileInfo; } public IEnumerable<FileInfo> FindAll(string directoryPath) { var directory = new DirectoryInfo(directoryPath); return directory.GetFiles("*.txt"); } } } To change the file content, use the regular expression Regex.Replace(text, "Шаблон", "Замена") .
String processing guidelines
- I'll check it in the morning, that is, where am I going to throw it namely into the rar archive, then exe should I replace the text with another text in all txt files? You wrote the code like that, right? - komra23
- Yes, just so just do not forget to run the program from the archive. - Andrei Pavlyuk
- @ May_be if txt files are in rar, then txt files need to be unpacked / extracted from the archive, - Stack
- @AndrewPavlyuk Of course, it is useful to destroy fresh minds with decisions, but still not to solve everything)))) - dirkgntly
- @dDevil Well, I left space for reflection in the context of regular expressions ...))) - Andrei Pavlyuk
|
меняет текст во всех txt-файлах куда я положу софт<- changes for what? And what was this in the file? Without an example, it will not work. - Neolisk%APPDATA%\Имя Вашего Приложения. - VladD