简体   繁体   English

C#文件行用arraylist删除

[英]C# file line remove with arraylist

I am trying to make a class which will help me delete one specific line from a file. 我正在尝试创建一个类,它将帮助我从文件中删除特定行。 So I came up with the idea to put all lines in an arraylist, remove from the list the line i don't need, wipe clear my .txt file and write back the remaining objects of the list. 因此,我想到了将所有行放入arraylist的想法,从列表中删除不需要的行,清除.txt文件并写回列表的其余对象。 My problem is that I encounter some sort of logical error i can't fint, that doesn't remove the line from the arraylist and writes it back again. 我的问题是我遇到某种我无法发现的逻辑错误,该错误不会从arraylist中删除该行并将其再次写回。 Here's my code: 这是我的代码:

public class delete
{
    public void removeline(string line_to_delete)
    {
        string[] lines = File.ReadAllLines("database.txt");
        ArrayList list = new ArrayList(lines);
        list.Remove(line_to_delete);
        File.WriteAllText("database.txt", String.Empty);
        using (var writer = new StreamWriter("database.txt"))
        {
            foreach (object k in lines)
            {
                writer.WriteLine(k);
            }
        }
    }
}

What is that I am missing? 我想念的是什么? I tried lots of things on removing a line from a text file that did not work. 我尝试了许多操作,从文本文件中删除行,但该行无效。 I tried this because it has the least file operations so far. 我尝试了此操作,因为到目前为止文件操作最少。

Thanks! 谢谢!

You can do: 你可以做:

var line_to_delete = "line to delete";
var lines = File.ReadAllLines("database.txt");
File.WriteAllLines("database.txt", lines.Where(line => line != line_to_delete));

File.WriteAllLines will overwrite the existing file. File.WriteAllLines将覆盖现有文件。

Do not use ArrayList , there is a generic alternative List<T> . 不要使用ArrayList ,而是通用的List<T> Your code is failing due to the use of ArrayList as it can only remove a single line matching the criteria. 您的代码由于使用ArrayList而失败,因为它只能删除符合条件的一行。 With List<T> you can use RemoveAll to remove all the lines matching criteria. 使用List<T>您可以使用RemoveAll删除所有符合条件的行。

If you want to do the comparison with ignore case you can do: 如果要使用忽略大小写进行比较,可以执行以下操作:

File.WriteAllLines("database.txt", lines.Where(line => 
            !String.Equals(line, line_to_delete, StringComparison.InvariantCultureIgnoreCase)));

I believe you're intending: 我相信您打算:

public static void RemoveLine(string line)
{
     var document = File.ReadAllLines("...");
     document.Remove(line);
     document.WriteAllLines("..");
}

That would physically remove the line from the collection. 那实际上会从集合中删除该行。 Another approach would be to simply filter out that line with Linq: 另一种方法是使用Linq过滤掉该行:

var filter = document.Where(l => String.Compare(l, line, true) == 0);

You could do the Remove in an ArrayList proper documentation on how is here . 您可以执行ArrayListRemove适当文档,以了解此处的操作 Your code should actually work, all of these answers are more semantic oriented. 您的代码实际上应该可以工作,所有这些答案都是面向语义的。 The issue could be due to actual data, could you provide a sample? 问题可能是由于实际数据所致,您能否提供样本? I can't reproduce with your code. 我无法复制您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM