Answers for "how to eliminate a line in a txt file using c#"

C#
0

delete the particular line in files in c#

string line = null;
int line_number = 0;
int line_to_delete = 12;

using (StreamReader reader = new StreamReader("C:\\input")) {
    using (StreamWriter writer = new StreamWriter("C:\\output")) {
        while ((line = reader.ReadLine()) != null) {
            line_number++;

            if (line_number == line_to_delete)
                continue;

            writer.WriteLine(line);
        }
    }
}
Posted by: Guest on March-16-2021
0

delete the particular line in files in c#

string line = null;
string line_to_delete = "the line i want to delete";

using (StreamReader reader = new StreamReader("C:\\input")) {
    using (StreamWriter writer = new StreamWriter("C:\\output")) {
        while ((line = reader.ReadLine()) != null) {
            if (String.Compare(line, line_to_delete) == 0)
                continue;

            writer.WriteLine(line);
        }
    }
}
Posted by: Guest on March-16-2021

Code answers related to "how to eliminate a line in a txt file using c#"

C# Answers by Framework

Browse Popular Code Answers by Language