C# 用于在文本文件中搜索精确字符串并在查找失败时执行操作的代码

C# 用于在文本文件中搜索精确字符串并在查找失败时执行操作的代码,c#,winforms,visual-studio-2015,C#,Winforms,Visual Studio 2015,因此,我编写了一些代码,假设读取一个文本文件,然后检查特定字符串的实例。如果该字符串存在,则需要执行操作。我添加了一些消息框,让我知道该功能的进度。问题是,它找不到这个词。这是我的密码: private void test2_Click(object sender, EventArgs e) { StreamReader objReader = new StreamReader("C:\\testing\\mslogon.log"); string sLine = "";

因此,我编写了一些代码,假设读取一个文本文件,然后检查特定字符串的实例。如果该字符串存在,则需要执行操作。我添加了一些消息框,让我知道该功能的进度。问题是,它找不到这个词。这是我的密码:

private void test2_Click(object sender, EventArgs e)
{
    StreamReader objReader = new StreamReader("C:\\testing\\mslogon.log");
    string sLine = "";
    ArrayList arrText = new ArrayList();
    MessageBox.Show("File Read");

    while (sLine != null)
    {
        sLine = objReader.ReadLine();
        if (sLine != null)
        {
            arrText.Add(sLine);
        }
    }

    if (arrText.Contains("Invalid"))
    {
        MessageBox.Show("Word Invalid Found");
    }
    else
    {
        MessageBox.Show("Nada");
    }
}

问题是,通过执行
arrText.Contains(“Invalid”)
,您正在搜索
数组的每个元素,以查找“Invalid”,因此除非您的文本文件中有一行包含该文本,否则将找不到它

您需要在构建阵列时搜索每一行,并在那里设置标志

var isFound = false;
var searchPhrase = "Invalid";

while (sLine != null)
{
    sLine = objReader.ReadLine();
    if (sLine != null)
    {
        arrText.Add(sLine);
        if (sLine.Contains(searchPhrase)) isFound = true;
    }
}

if (isFound)
{
    MessageBox.Show("Word Invalid Found");
}
else
{
    MessageBox.Show("Nada");
}

现在,您正在检查arrText中是否有完全等于“Invalid”的元素。如果“Invalid”嵌入到某个较大的字符串中,它将找不到它

要检查任何字符串是否包含无效内容,必须执行以下操作:

foreach(var line in arrText)
{
    if (line.Contains("Invalid")) { MessageBox.Show("Word 'Invalid' Found"); break; }
}
使用@Alexei提到的File.ReadAllText可以让你做你想做的事情,看起来像这样:

String wholeFile = File.ReadAllText();
if (wholeFile.Contains("Invalid")) { 
    MessageBox.Show("Word 'Invalid' Found"); 
} else {
. . .

如果您的文件不是很大,请考虑使用函数一次读取所有文本。< /P> 此外,默认情况下,搜索是以区分大小写的方式进行的,因此您将错过“word invalid found”(找到无效单词)。

请尝试以下操作:

private void test2_Click(object sender, EventArgs e)
{
    StreamReader objReader = new StreamReader("C:\\testing\\mslogon.log");
    string sLine = "";
    ArrayList arrText = new ArrayList();
    MessageBox.Show("File Read");

    while (sLine != null)
    {
        sLine = objReader.ReadLine();
        if (sLine != null)
        {
            arrText.Add(sLine);
        }
    }
    foreach(string x in arrText)
    {
      if (x.Contains("Word Invalid Found"))
      {
        MessageBox.Show("Nothing Found");
      }
      else
      {
        MessageBox.Show("Nada");
      }
    }
}

您必须在数组的每一项中进行搜索,在实际代码中,只有内容为“Invalid”的字符串才能工作

使用LINQ,您可以执行以下操作:

    if (arrText
        .ToArray()
        .Where(p => ((string)p).Contains("Invalid")).Any())
    {
        MessageBox.Show("Word Invalid Found");
    }

你的代码做了太多的工作。如果您只想知道文件中是否包含任何带有“Invalid”一词的行,则不必一次读取整个文件。你可以停在你找到的第一行。考虑这一点:

bool found = false;
var lines = File.ReadLines(@"c:\testing\mslogon.log");
foreach (var sLine in lines)
{
    if (sLine.IndexOf("Invalid") != -1)
    {
        found = true;
        break;
    }
}
if (found)
{
    MessageBox.Show("Found it!");
}
else
{
    MessageBox.Show("Not found.");
}
并且,您可以用LINQ表达式替换该
foreach

bool found = lines.Where(sLine => sLine.IndexOf("Invalid") != -1).Any();

这里的关键是
File.ReadLines
创建一个枚举器,允许您一次读取一行文件,而不是将整个文件加载到内存中。在循环中,如果找到行,
break
会提前终止循环。在
LINQ
表达式中,
Any
方法基本上做相同的事情。关键是,如果在第一行中发现“Invalid”,则无需阅读文件的其余部分。

好的,如果该行包含的内容超过“Invalid”一词,那么这是否有效,或者我是否需要执行其他操作?我不知道,这取决于您是否关心它是否包含多个无效内容。这两种方法都可以。这看起来是我的最佳解决方案。它避免了将文件内容转储到数组中,并立即获得答案。