How to Delete a Line from a Text File in C#

How to delete a line from a text file in C#?

Read the file, remove the line in memory and put the contents back to the file (overwriting). If the file is large you might want to read it line for line, and creating a temp file, later replacing the original one.

How to delete a line of text file

This is in winform. textbox1 is where you enter your word and textbox2 will display the message.

textBox2.Clear();
if (!string.IsNullOrWhiteSpace(textBox1.Text))
{
string LinesToDelete = textBox1.Text;
var Lines = File.ReadAllLines(@"E:\sample.txt");
if (Lines.Contains(textBox1.Text))
{
var newLines = Lines.Where(line => !line.Contains(LinesToDelete));
File.WriteAllLines(@"E:\sample.txt", newLines);
textBox2.Text = "Removed";
}

else
{
textBox2.Text = "Not found";
}
}

If you do not need to show the message, the code below will be sufficient.

var Lines = File.ReadAllLines(@"E:\sample.txt");
var newLines = Lines.Where(line => !line.Contains(LinesToDelete));
File.WriteAllLines(@"E:\sample.txt", newLines);

Delete specific line from a text file?

If the line you want to delete is based on the content of the line:

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);
}
}
}

Or if it is based on line number:

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);
}
}
}

Delete specific line in a text file , using Sytem.IO

It is not possible to edit the contents of a text file inside disk. you have to overwrite the file again.

Also you can convert your array into list and use List(T).Remove method to remove first matched item from it.

string[] inventoryData = File.ReadAllLines("Inventory.txt");
List<string> inventoryDataList = inventoryData.ToList();

if (inventoryDataList.Remove(txtSearch.Text)) // rewrite file if one item was found and deleted.
{
System.IO.File.WriteAllLines("Inventory.txt", inventoryDataList.ToArray());
}

If you want to remove all items in one search then use List<T>.RemoveAll method.

if(inventoryDataList.RemoveAll(str => str == txtSearch.Text) > 0) // this will remove all matches.

Edit: for older .Net Framework versions (3.5 and lower) you have to call ToArray() because WriteAllLines only takes array for second argument.

How can i delete a line from text file by the line number ? And delete item from ListView by index number?

    List<string> lines = File.ReadAllLines("c:\\a.txt").ToList();
lines.RemoveAt(linenumber);

Listview1.Items.RemoveAt(linenumber);

Efficient way to delete a line from a text file

The most straight forward way of doing this is probably the best, write the entire file out to a new file, writing all lines except the one(s) you don't want.

Alternatively, open the file for random access.

Read to the point where you want to "delete" the line.
Skip past the line to delete, and read that number of bytes (including CR + LF - if necessary), write that number of bytes over the deleted line, advance both locations by that count of bytes and repeat until end of file.

Hope this helps.

EDIT - Now that I can see your code

if (!_deletedLines.Contains(counter)) 
{
writer.WriteLine(reader.ReadLine());
}

Will not work, if its the line you don't want, you still want to read it, just not write it. The above code will neither read it or write it. The new file will be exactly the same as the old.

You want something like

string line = reader.ReadLine();
if (!_deletedLines.Contains(counter))
{
writer.WriteLine(line);
}

Delete multiple lines from text file using c#

The logic of your current code only deletes the line equal to line_to_delete To resolve you would just need to change your if statement.

if (line_number > 20)  //i would suggest not hardcoding 20
{
writer.WriteLine(line);
}


Related Topics



Leave a reply



Submit