简体   繁体   English

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

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

I am trying to find out if a text file contains a string . 我试图找出文本文件是否包含string The string could be any of the string 's that are contained within the list textwords , if a word is contained within the text file then it will copy the file it to a new location string可以是列表textwords中包含的任何string ,如果文本文件中包含一个单词,则它将文件复制到新位置

The issue I am having is that the program does not loop through all the words in textwords it only takes the first value of the list. 我遇到的问题是该程序不会循环遍历textwords所有单词,它仅采用列表的第一个值。 How do I get it to loop through all the strings within the list and see if they are contained within the text file before copying the file. 在复制文件之前,如何使它遍历列表中的所有字符串并查看它们是否包含在文本文件中。

Any help would be appreciated. 任何帮助,将不胜感激。

My code is below, 我的代码如下

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

}

You have 'break' statements in both the 'if' and 'else' conditions. 您在“ if”和“ else”条件下都有“ break”语句。 Therefore it will never loop beyond the first string. 因此,它永远不会循环到第一个字符串之外。

But you are performing the copy and break in the first pass 但是您正在执行复制并在第一遍中中断
No kidding it does not get to the second word 不开玩笑就说不准第二句话

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

You are using break at the end of the statements. 您在语句末尾使用break This will break the loop. 这将打破循环。 You should use continue instead. 您应该改为使用continue

But this continue will be useless since you're using just an if-else function to do copy the file and nothing more. 但是,这种continue将毫无用处,因为您仅使用if-else函数来复制文件,仅此而已。 You can get rid of the break in this case. 在这种情况下,您可以摆脱break The program would execute the if statement and at the end of the block it will ignore the else statement (or vice-versa) and iterate the loop. 程序将执行if语句,并在该块的末尾忽略else语句(反之亦然)并循环执行。

You have "break" causing it to stop after first iteration. 您有“中断”,使其在第一次迭代后停止。 Also a search like this wouldn't work. 同样,这样的搜索也不起作用。 A better (yet not perfect) one is: 更好(但还不完美)的是:

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