How to delete a specific line from a file?

2

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

I have this text in a file .txt :

linha1
linha2
linha3

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

If the user deletes "line2" 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 it and I do not understand some tutorials that appear to me.

    
asked by anonymous 09.08.2016 / 14:54

2 answers

1

So:

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: link

    
09.08.2016 / 16:45
0

To load the file into 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 the ListBox of the file, you will have to read line by line of the file and compare it with the selected line of ListBox , done this, save the modifications in a temporary file, modified file.

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){
        }
    }
}
    
09.08.2016 / 15:01