check if string exists in a file

后端 未结 7 1693
猫巷女王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:17

    You could try this code:

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

提交回复
热议问题