Check if line exists in file

I have the following code snippet that opens a text file and reads all the lines in a file and saves it to an array of strings.

It then checks to see if a string is present in the array. However, the problem I am facing is that whenever a line is found, it always shows “there is a match” and also “there is no match”. Any idea how to fix this?

check this code:

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");
    }
}
+4
source share
6 answers

I would recommend setting and setting the 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

+3

, -, , . :

if(File.ReadAllText(path).Contains(domain))
{
    MessageBox.Show("There is a match");
}
+19

. File.ReadLines, , .

private bool DomainExists(string domain)
{
    foreach(string line in File.ReadLines(path))
        if (domain == line)
            return true; // and stop reading lines

    return false;
}

:

if (DomainExists(domain))
    MessageBox.Show("there is a match");
else
    MessageBox.Show("there is no match");

- StreamReader, File.ReadAllLines ( ). - sr . - , using. .

+2
source

The simplest way:

string content = File.ReadAllText(path);
if (content.IndexOf(domain) > -1)
{
   // domain exists
}
else
{
   // domain does not exist
}

and now analyze your code:

Third, you create an instance of StreamReader, but do not use it later in your code.

2nd, what if a domain name has multiple occurrences in a file? In your code, you will get some “matches” in your code.

using (StreamReader sr = File.OpenText(path)) // you can remove this line
{
    string[] lines = File.ReadAllLines(path); // as you are not using it here
    for (int x = 0; x < lines.Length - 1; x++)
    {
        if (domain == lines[x])
        {
            sr.Close();
            MessageBox.Show("there is a match");
            hasMatch = true;
            break; // exit loop if found
        }
    }

    if (!hasMatch)
    {
        // there is no match
    }

    if (sr != null) // you dont need this if you remove it from the beginning of the code
    {
        sr.Close();
        MessageBox.Show("there is no match");
    }
}
0
source

You can 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");
                            }
                        }
-1
source

Try to catch:

string x;

string log = @"C:\Users\Log.txt";

string ruta = @"C:\Users\x.txt";

if (File.Exists(ruta))
{                    
    try
    {
        x = File.ReadAllText(ruta);  
    }
    catch (Exception ex)
    {
        File.AppendAllText(ruta, "Something");
        File.AppendAllText(log, Environment.NewLine + DateTime.Now.ToString() + ": The file not contain a string. " + ex.Message);
    }
}
-1
source

All Articles