How to delete a specific line from a file?

Asked

Viewed 2,514 times

2

My goal is that when the user removes an item from a Listbox, the program delete a certain line from a file.


I have this text in a file .txt:

linha1
linha2
linha3

In the Listbox contains the contents of the file .txt, I want that when the user deletes a specific line, delete also in the file.

If the user deletes "Linha2" the file .txt should look like this:

linha1
linha3

I have this code so far:

//delete ListItemsBox Selected Item
private void ListItemsBox_DoubleClick(object sender, EventArgs e)
{
    string path = AppDomain.CurrentDomain.BaseDirectory.ToString() + "MyTest.cs";
    if (ListItemsBox.SelectedItem.Equals("When Start"))
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?All items will be deleted including all files!", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
            ListItemsBox.Items.Clear();
            File.Delete(path);
            btnWhenStart.Show();
            End.Show();
        }
        else if (dialogResult == DialogResult.No)
        {

        }
    }
    else
    {
        DialogResult dialogResult = MessageBox.Show("Are you sure you want to delete this item?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
        if (dialogResult == DialogResult.Yes)
        {
                    ListItemsBox.Items.Remove(ListItemsBox.SelectedItem);
        }
        else if (dialogResult == DialogResult.No)
        {

        }
    }

}

The problem I am facing is that I do not know how to do and do not realize some tutorials that appear to me.

  • I edited your question to make it clearer what you want. Note: In your code will only delete the line if it is "When Start" and not the selected line.

2 answers

1


Thus:

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

I found in Stackoverflow in English: https://stackoverflow.com/questions/1245243/delete-specific-line-from-a-text-file

0

To upload the file to Listbox, do so:

string caminho = AppDomain.CurrentDomain.BaseDirectory.ToString() + "foobar.txt";
listBox1.Items.Clear();

if (File.Exists(caminho))
{
     string[] linhas = File.ReadAllLines(caminho);
     listBox1.Items.AddRange(linhas);
}

To remove the selected line in ListBox of the file, you will have to read row by row of the file and compare with the selected row of the ListBox, done this, save the modifications in a temporary file, in the end replace the original file with the modified.

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    string caminho = AppDomain.CurrentDomain.BaseDirectory.ToString() + "foobar.txt";
    int indice = listBox1.SelectedIndex;

    if (indice != -1){
        string linha = listBox1.Items[indice].ToString();

        DialogResult dialogResult = MessageBox.Show(string.Format("Deseja remover {0}?", linha),
                                                    "Warning",
                                                    MessageBoxButtons.YesNo,
                                                    MessageBoxIcon.Warning);

        if (dialogResult == DialogResult.Yes){
            using (var input = File.OpenText(caminho))
            using (var output = new StreamWriter("tmpFoo.txt")){
                string linhaAtual;
                while ((linhaAtual = input.ReadLine()) != null) {
                    if (linhaAtual != linha){
                            output.WriteLine(linhaAtual);
                    } 
                }
            }

            listBox1.Items.RemoveAt(indice); // Remove o item selecionado
            File.Delete(caminho); // Deleta o arquivo original
            File.Move("tmpFoobar.txt", caminho); // Substitui o original pelo modificado
        }
        else if (dialogResult == DialogResult.No){
        }
    }
}
  • I can remove from the list box I want is that the Lihna leaves the . Cs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.