tog*_*rli 0 c# wpf listbox winforms
我有 listBox 和这个文本文件: " 1 2 3 4 'blank line' " 我想读取这个没有空行的文件。我试试这个:
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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用
你可以只File.ReadAllLines使用Where
例子
var listOfLines = File.ReadAllLines(path)
.Where(x => !string.IsNullOrWhiteSpace(x));
// add items to list box here
Run Code Online (Sandbox Code Playgroud)
打开一个文本文件,将文件的所有行读入字符串数组,然后关闭文件。
String.IsNullOrWhiteSpace(String) 方法
指示指定的字符串是否为 null、空或仅由空白字符组成。
根据谓词过滤值序列。