C# 避免将相等的现有行写入文本文档

C# 避免将相等的现有行写入文本文档,c#,C#,我是C#的新手,需要你的帮助 在这段代码中,我使用循环将一些单词或短语写入文本文档。我的问题是如何避免在输出上显示文本文档中已经存在并在文本文档中找到的等分线 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace program_1 { class Prog

我是C#的新手,需要你的帮助

在这段代码中,我使用循环将一些单词或短语写入文本文档。我的问题是如何避免在输出上显示文本文档中已经存在并在文本文档中找到的等分线

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace program_1
{
    class Program
    {
        static void Main(string[] args)
        {

            int i = 0;

            for (;;)
            {

                Console.Write("Write phrase: ");
                var row = Console.ReadLine();

                Console.WriteLine(i++ + ". " + (row));

                TextWriter tsw = new StreamWriter("lines.txt", true);

                tsw.WriteLine(row);

                tsw.Close();

            }

            Console.Read();
        }
    }
}

如果要避免在文件中写入重复行,则需要另一种方法。首先,您需要将所有行都存储在内存中,然后检查用户是否已在内存中输入了一行,然后在程序末尾写入所有内容

class Program
{
    static void Main(string[] args)
    {

        int i = 0;
        List<string> wordsTyped = new List<string>();

        // If the file already exists then you can load its content 
        // in memory to start your checks against the current content
        // of the file....
        if(File.Exists("lines.txt"))
            wordsTyped.AddRange(File.ReadAllLines("lines.txt"));

        for (;;)
        {

            Console.Write("Write phrase: (type 'exit' to end)");
            string row = Console.ReadLine();

            // Provide a way to exit from this infinite loop
            if(row == "exit")
                break;

            Console.WriteLine(i++ + ". " + (row));

            // Use IndexOf to find if there is a match for your row 
            // and in which position in the List<string>
            int position = wordsTyped.IndexOf(row);
            if (position != -1)
                Console.WriteLine($"Already inserted. Found match at line {position+1} , type again");
            else
            {
                wordsTyped.Add(row);

                // It of uttermost importance to enclose the StreamWriter
                // in a using statement to be sure to close and dispose it
                // after the write, otherwise you could lock yourself out
                using(StreamWriter sw = File.AppendText("lines.txt"))
                  sw.WriteLine(row);
            }
        }
        // File.WriteAllLines("lines.txt", wordsTyped.ToArray());
        Console.Read();
    }
}
类程序
{
静态void Main(字符串[]参数)
{
int i=0;
List wordsTyped=新列表();
//如果文件已存在,则可以加载其内容
//在内存中启动对当前内容的检查
//文件的名称。。。。
如果(File.Exists(“lines.txt”))
AddRange(File.ReadAllLines(“lines.txt”);
对于(;;)
{
控制台。写入(“写入短语:(在末尾键入“退出”);
string row=Console.ReadLine();
//提供一种退出此无限循环的方法
如果(行==“退出”)
打破
Console.WriteLine(i++“+(行));
//使用IndexOf查找您所在行是否存在匹配项
//在列表中的哪个位置
int position=wordsTyped.IndexOf(行);
如果(位置!=-1)
WriteLine($”已插入。在第{position+1}行找到匹配项,请再次键入”);
其他的
{
添加(行);
//最重要的是把这名作家关起来
//在using语句中,确保关闭并处置它
//写完之后,否则你会把自己锁在外面
使用(StreamWriter sw=File.AppendText(“lines.txt”))
西南写入线(世界其他地区);
}
}
//writeAllines(“lines.txt”,wordsTyped.ToArray());
Console.Read();
}
}

您错过了上次
控制台之后的
中断
。WriteLine
。和花括号:)@HamletHakobyan我想OP想要继续输入,但你有一点。我的意思是
继续
:)@Steve_u你好Steve谢谢你的反馈,是的,这种方式可以按原样工作,但对我来说不是这样,每次写作我都需要这个,不是在退出之后。所以,如果文件中已经存在相等的行,首先我希望避免将这一行写入文件,但我希望每次都从文档的输出中获取这一行,因为我还希望获取位于找到的行旁边或之前的行。关于最后一个问题,我在这里有个问题,但仍然不知道怎么做:@Steve yes有效,谢谢你的支持。删除问题