Reputation: 302
I have listBox and this text file: " 1 2 3 4 'blank line' " I want to read this file without blank line. I try this:
using (StreamReader reader = new StreamReader("MyMessages.chat"))
{
var line = reader.ReadToEnd().Split('\n');
for (int i = 0; i < line.Length; i++)
{
if (line[i] != " ")
{
listBox.Items.Add(line[i]);
listBox.Visibility = Visibility.Visible;
}
}
}
But it doesn't work
Upvotes: 0
Views: 2999
Reputation: 81483
You could just use File.ReadAllLines
with a Where
Example
var listOfLines = File.ReadAllLines(path)
.Where(x => !string.IsNullOrWhiteSpace(x));
// add items to list box here
Opens a text file, reads all lines of the file into a string array, and then closes the file.
String.IsNullOrWhiteSpace(String) Method
Indicates whether a specified string is null, empty, or consists only of white-space characters.
Filters a sequence of values based on a predicate.
Upvotes: 5