C# program to delete specific line from text file | Easy solution

├Źndice
  1. Introduction
  2. The Solution
  3. Conclusion

Introduction

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.

The Solution

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.

Conclusion

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.

Click to rate this post!
[Total: 0 Average: 0]

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up

Below we inform you of the use we make of the data we collect while browsing our pages. You can change your preferences at any time by accessing the link to the Privacy Area that you will find at the bottom of our main page. More Information