C# program to delete specific line from text file | Easy solution
When working with text files in C#, it is common to need to delete a specific line from the file. While this may seem like a daunting task, there is actually a simple solution that can accomplish this.
To delete a specific line from a text file in C#, we can use the following code:
string lines = File.ReadAllLines("file.txt"); lines = lines.Where(line => !line.Contains("delete this line")).ToArray(); File.WriteAllLines("file.txt", lines);
This code first reads all the lines from the text file into a string array. It then uses LINQ to filter out the line that contains the text we want to delete. Finally, it writes the updated string array back to the text file.
With just a few lines of code, we can easily delete a specific line from a text file in C#. This solution is both simple and efficient, making it a great choice for any C# project that needs to work with text files.