繁体   English   中英

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

[英]finding a string in a text file contained within a List<string>

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

我遇到的问题是该程序不会循环遍历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”语句。 因此,它永远不会循环到第一个字符串之外。

但是您正在执行复制并在第一遍中中断
不开玩笑就说不准第二句话

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());
    }           
}

您在语句末尾使用break 这将打破循环。 您应该改为使用continue

但是,这种continue将毫无用处,因为您仅使用if-else函数来复制文件,仅此而已。 在这种情况下,您可以摆脱break 程序将执行if语句,并在该块的末尾忽略else语句(反之亦然)并循环执行。

您有“中断”,使其在第一次迭代后停止。 同样,这样的搜索也不起作用。 更好(但还不完美)的是:

foreach (FileInfo file in files)
{
    //reads the file contents
    var content = File.ReadAllText(file.FullName);

    if (textwords.Any(tw => Regex.IsMatch(content, @"\b" + tw.Trim() + @"\b", RegexOptions.IgnoreCase))
    {
        try
        {
            File.Move(file.FullName, Path.Combine(@"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Quarantin_Messages", file.Name));
        }
        catch (IOException cannot_Move_File)
        {
            MessageBox.Show(cannot_Move_File.ToString());
        }
    }
    else
    {
        //else it is moved To valid messages
        try
        {
            File.Copy(file.FullName, @"F:\UNI\Year 2\Tri 2\Software Engineering Methods\Coursework\Noogle system\Valid_Messages\" + file.Name);
        }
        catch (IOException cannot_Move_File)
        {
            MessageBox.Show(cannot_Move_File.ToString());
        }
    }
}

暂无
暂无

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

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