小编Bry*_*est的帖子

检查文件中是否存在字符串

我有以下代码,它打开一个文本文件并读取文件中的所有行并将其存储到字符串数组中.

然后检查字符串是否存在于数组中.然而,我面临的问题是,每当找到一个字符串时,它总是显示"有匹配"以及"没有匹配".知道如何解决这个问题吗?

检查此代码:

using (StreamReader sr = File.OpenText(path))
{
    string[] lines = File.ReadAllLines(path);
    for (int x = 0; x < lines.Length - 1; x++)
    {
        if (domain == lines[x])
        {
            sr.Close();
            MessageBox.Show("there is a match");
        }
    }
    if (sr != null)
    {
        sr.Close();
        MessageBox.Show("there is no match");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# string streamreader

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

c# ×1

streamreader ×1

string ×1