Reputation: 81
I have an issue with a SteamReader. My StreamReader won't find a specific string match in a file even though I know the string exists. The best way to show this is through an image. You would get a better understanding of how it works. I will provide code after:
The Image shows the code which checks to see if the string is in the file. The open text document is the file that is checked. This is passed as a variable when the new form (this form) is create after inputting the information.The form button is the button which runs the code as shown. If the string is not found, the message box is shown.
As you can see the file contians the string but simply doesn't pick it up :/
I suspect this is something very simple but I need a fresh pair of eyes. Here is the code:
private void btnGetActivities_Click(object sender, EventArgs e)
{
if (File.Exists(sVenueName.ToString() + ".txt"))
{
using (StreamReader RetrieveEvents = new StreamReader(sVenueName.ToString() + ".txt")) //Create a new file with the name of the username variable
{
string EventString;
string EventType;
string EventPeopleAttending;
string line = RetrieveEvents.ReadLine(); //Declare string variable to hold each line in the file
while (RetrieveEvents.Peek() != -1)
{
if (line.Contains("Event Name:")) //When this line is found,
{
EventString = line.Remove(0, 12); //Remove the characters from the line and store it in a variable
lstDisplayActivities.Items.Add(line);
line.Skip(1).Take(2);
EventType = line.Remove(0, 12);
lstDisplayActivities.Items.Add(line);
line.Skip(2).Take(3);
EventPeopleAttending = line.Remove(0, 18);
lstDisplayActivities.Items.Add(line);
}
else
{
MessageBox.Show("No Files were found");
}
}
}
}
}
Upvotes: 1
Views: 479
Reputation: 3417
To elaborate on what @CNuts said in the comments
if (line.Contains("Event Name:")) //When this line is found,
{
//Do stuff
}
else
{
MessageBox.Show("No Files were found");
}
Since not all the lines in the file contain "Event Name:" every time the line does not contain this particular string you will get the message "No Files were found".
Here's a suggestion:
bool eventsFound = false
while (RetrieveEvents.Peek() != -1)
{
if (line.Contains("Event Name:")) //When this line is found,
{
//Do stuff
eventsFound = true;
}
line = RetrieveEvents.ReadLine();
}
if(!eventsFound)
MessageBox.Show("No Files were found");
Upvotes: 0
Reputation: 216313
You are reading only the first line from the StreamReader. The other lines are never read. You need a loop and at each loop read again a line
string line = RetrieveEvents.ReadLine();
while (RetrieveEvents.Peek() != -1)
{
if (line.Contains("Event Name:")) //When this line is found,
{
EventString = line.Remove(0, 12);
lstDisplayActivities.Items.Add(line);
}
else if(line.Contains("Event Type:"))
{
EventType = line.Remove(0, 12);
lstDisplayActivities.Items.Add(line);
}
else if(line.Contains("People Attending:"))
{
EventPeopleAttending = line.Remove(0, 18);
lstDisplayActivities.Items.Add(line);
}
// This reads the next line.....
line = RetrieveEvents.ReadLine()
}
}
The lines
line.Skip(1).Take(2)
and the likes don't advance to the next lines but they simply skip the number of chars in line (1) and then take the following 2 chars just to discard everything you don't assign it to anything
Upvotes: 1