C# 对照列表检查文本文件的内容<;字符串>;

C# 对照列表检查文本文件的内容<;字符串>;,c#,list,filereader,C#,List,Filereader,因此,我试图检查文本文件的内容,以查看列表textwords中包含的任何值是否存在于文本文件中 但是,当执行代码时,它总是认为消息不包含textwords列表中包含的任何字符串 使用的代码如下 在此方面的任何帮助都将不胜感激 List<string> textwords = new List<string>(); using (var UnacceptableWords = new StreamReader("fileLocation"))

因此,我试图检查文本文件的内容,以查看列表
textwords
中包含的任何值是否存在于文本文件中

但是,当执行代码时,它总是认为消息不包含
textwords
列表中包含的任何字符串

使用的代码如下

在此方面的任何帮助都将不胜感激

List<string> textwords = new List<string>();
        using (var UnacceptableWords = new StreamReader("fileLocation"))
        {
            while (!UnacceptableWords.EndOfStream)
            {
                string[] row = UnacceptableWords.ReadLine().Split(',');
                string Column1 = row[0];

                textwords.Add(Column1);
            }
        }

        directory = new DirectoryInfo("filelocation");
        files = directory.GetFiles("*.txt");
        foreach (FileInfo file in files)
        {
            using(StreamReader Message = new StreamReader(file.FullName))
            {
                string MessageContents = Message.ReadToEnd();
                if(MessageContents.Contains(textwords.ToString()))
                {
                    MessageBox.Show("found a word");
                }
                MessageBox.Show("message clean");
            }
        }
List textwords=new List();
使用(var UnacceptableWords=newstreamreader(“fileLocation”))
{
而(!UnacceptableWords.EndOfStream)
{
string[]行=不可接受的单词.ReadLine().Split(',');
字符串Column1=行[0];
text words.Add(第1列);
}
}
directory=newdirectoryinfo(“filelocation”);
files=directory.GetFiles(“*.txt”);
foreach(文件中的文件信息文件)
{
使用(StreamReader Message=newstreamreader(file.FullName))
{
字符串MessageContents=Message.ReadToEnd();
if(MessageContents.Contains(textwords.ToString()))
{
Show(“找到一个单词”);
}
MessageBox.Show(“消息清理”);
}
}
方法
string.Cointains()
接收一个字符串,但您正在向其中传递已转换为字符串的
列表

List.ToString()!=列表中作为字符串包含的值

要做到这一点,您必须遍历数组,并一次传递数组中的每个元素

foreach(string keyword in textwords)
{
    if(MessageContents.Contains(keyword))
    {
         MessageBox.Show("found a word");
         break;
    }
}

不,你犯的错误和原来的问题一样。只是你做了很多次。测试应该是
if(MessageContents.Contains(keyword))
哦,对不起,我弄错了,我复制了if,忘了更改它,现在将进行编辑。谢谢你指出,这是我原来的意图,我们不能做这件事吗?也要考虑<代码>包含< /代码>尊重外壳。因此
“QuickBrownFox”。包含(“fox”)
返回
false
!对不起,那是我的错误,它确实起作用了,我还没有看完所有的话谢谢你的帮助!