查找包含字符串的行并在.txt文件中删除该行

时间:2014-01-31 19:38:43

标签: c# text

我的目标是删除与字符串DonatorPlayer

匹配的行
if (targetPlayer == null)
{
    player.Message("That is not online. Please try again when they are online so that they may be notified in due time.");
}
else
{
    //switch (DonationCheck)
    //{
        foreach (string line in lines)
        {
            WhichLoopDelete += 1;
            if (line == DonatorPlayer)
            {
                switch (DonationCheck)
                {
                    case 0:
                        lines[WhichLoopDelete] = null;
                        File.Delete("Donators.txt");
                        File.Create("Donators.txt");

                        DonationCheck = 1;
                        break;
                    case 1:
                        TextWriter tw = new StreamWriter("Donators.txt");
                        //Here is where I need to delete the line
                        break;

                }
            }
        }
    //}
}

1 个答案:

答案 0 :(得分:0)

只需使用:

lines = lines.Where(line => !line.Equals("DonatorPlayer")).ToArray();