check if string exists in a file

后端 未结 7 1686
猫巷女王i
猫巷女王i 2021-02-09 19:36

I have the following piece of code which opens a text file and reads all the lines in the file and storing it into a string array.

Which then checks if

7条回答
  •  日久生厌
    2021-02-09 20:15

    I would recommend seting and flag and checking it as follows...

    using (StreamReader sr = File.OpenText(path))
    {
        string[] lines = File.ReadAllLines(path);
        bool isMatch = false;
        for (int x = 0; x < lines.Length - 1; x++)
        {
            if (domain == lines[x])
            {
                sr.Close();
                MessageBox.Show("there is a match");
                isMatch = true;
            }
        }
        if (!isMatch)
        {
            sr.Close();
            MessageBox.Show("there is no match");
        }
    }
    

    Good Luck!

提交回复
热议问题