Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在列表中包含的文本文件中查找字符串<;字符串>;_C#_String - Fatal编程技术网

C# 在列表中包含的文本文件中查找字符串<;字符串>;

C# 在列表中包含的文本文件中查找字符串<;字符串>;,c#,string,C#,String,我试图找出文本文件是否包含字符串。字符串可以是列表文本单词中包含的任何字符串,如果文本文件中包含一个单词,则它会将该文件复制到新位置 我遇到的问题是,程序不会循环遍历textwords中的所有单词,它只获取列表的第一个值。在复制文件之前,如何让它遍历列表中的所有字符串并查看它们是否包含在文本文件中 任何帮助都将不胜感激 我的代码在下面 foreach (FileInfo file in files) { //reads the file contents bool nextout

我试图找出文本文件是否包含
字符串。
字符串
可以是列表
文本单词
中包含的任何
字符串
,如果文本文件中包含一个单词,则它会将该文件复制到新位置

我遇到的问题是,程序不会循环遍历
textwords
中的所有单词,它只获取列表的第一个值。在复制文件之前,如何让它遍历列表中的所有字符串并查看它们是否包含在文本文件中

任何帮助都将不胜感激

我的代码在下面

foreach (FileInfo file in files)
{
    //reads the file contents
    bool nextouterloop = false;
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        //checks if the textwords are present in the file


        foreach ( string Keyword in textwords )
        {

            //if they are file is moved to quarantine messages

            if ( MessageContents.Contains(Keyword) )
            {
                try
                {
                    File.Copy(file.FullName,
                        @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages\" +
                        file);

                }
                catch ( IOException cannot_Move_File )
                {
                    MessageBox.Show(cannot_Move_File.ToString());
                }
                break;
            }
            //else it is moved To valid messages
            else
            {
                try
                {
                    File.Copy(file.FullName,
                        @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\" +
                        file);
                }
                catch ( IOException cannot_Move_File )
                {
                    MessageBox.Show(cannot_Move_File.ToString());
                }
                break;
            }
        }
    }

}

在“if”和“else”条件下都有“break”语句。因此,它不会循环超过第一个字符串。

在“if”和“else”条件下都有“break”语句。因此,它永远不会循环超过第一个字符串。

您正在语句末尾使用
break
。这将打破循环。您应该改用
继续


但是这个
继续
将是无用的,因为您只使用if-else函数来复制文件,其他什么都没有。在这种情况下,您可以去掉
中断
。程序将执行if语句,在块的末尾它将忽略else语句(反之亦然),并迭代循环。

您正在语句末尾使用
break
。这将打破循环。您应该改用
继续

但是这个
继续
将是无用的,因为您只使用if-else函数来复制文件,其他什么都没有。在这种情况下,您可以去掉
中断
。程序将执行if语句,在块的末尾它将忽略else语句(反之亦然),并迭代循环。

您有“break”导致它在第一次迭代后停止。像这样的搜索也不起作用。更好(但不完美)的方法是:

您有一个“中断”导致它在第一次迭代后停止。像这样的搜索也不起作用。更好(但不完美)的方法是:


但是您在第一次通过时正在执行复制和中断
别开玩笑了,这还没到第二个字

foreach (FileInfo file in files)
{    
    string path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\";
    //reads the file contents
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        //checks if the textwords are present in the file               
        foreach (string Keyword in textwords)
        {
            if (MessageContents.Contains(Keyword))
            {
                path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages\"
                break;
            }
        }                 
    }
    // let the StreamReader close down before the copy  
    // don't need it anymore  
    try
    {
        File.Copy(file.FullName, path + file);
    }
    catch (IOException cannot_Move_File)
    {
        MessageBox.Show(cannot_Move_File.ToString());
    }           
}

但是您在第一次通过时正在执行复制和中断
别开玩笑了,这还没到第二个字

foreach (FileInfo file in files)
{    
    string path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\";
    //reads the file contents
    using (StreamReader ReadMessage = new StreamReader(file.FullName))
    {
        String MessageContents = ReadMessage.ReadToEnd();
        //checks if the textwords are present in the file               
        foreach (string Keyword in textwords)
        {
            if (MessageContents.Contains(Keyword))
            {
                path = @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages\"
                break;
            }
        }                 
    }
    // let the StreamReader close down before the copy  
    // don't need it anymore  
    try
    {
        File.Copy(file.FullName, path + file);
    }
    catch (IOException cannot_Move_File)
    {
        MessageBox.Show(cannot_Move_File.ToString());
    }           
}

如何声明和创建
textwords
?您确定它包含多个
字符串
值吗?如果以break结尾,则break将结束循环。如何声明和创建
textwords
?你确定它包含多个
string
值吗?break将结束循环,如果其他两个都以break结束。一般来说,我会尽量避免使用“假定”规范,特别是当它不是当前的问题时。你是在假设他的规范需要删减关键字,并且它必须是一个完整的单词(他们的关键字应该在单词边界上)。这样想是有道理的,但是您正在更改他的代码应该做什么(同样,在与问题无关的部分),并且没有对这些更改给出任何解释。字符串已经处于需要的状态,这是在将它们添加到列表中时完成的。无论如何,谢谢你的建议,我现在已经解决了这个问题。一般来说,我会尽量避免“假设”规范,特别是当它不是手头的问题时。你是在假设他的规范需要删减关键字,并且它必须是一个完整的单词(他们的关键字应该在单词边界上)。这样想是有道理的,但是您正在更改他的代码应该做什么(同样,在与问题无关的部分),并且没有对这些更改给出任何解释。字符串已经处于需要的状态,这是在将它们添加到列表中时完成的。无论如何,谢谢你的建议,我现在已经解决了这个问题。谢谢!我没有意识到,将
中断设置在我设置的位置会阻止列表的循环。谢谢!我没有意识到,将
中断放在我所做的位置会停止列表的循环。