The simplest way:
string content = File.ReadAllText(path);
if (content.IndexOf(domain) > -1)
{
}
else
{
}
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))
{
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");
hasMatch = true;
break;
}
}
if (!hasMatch)
{
}
if (sr != null)
{
sr.Close();
MessageBox.Show("there is no match");
}
}
source
share