<分区>
当我在列表框中显示它们时,我只是写了“(Collection)”而不是实际文本。
标签 c# visual-studio
<分区>
当我在列表框中显示它们时,我只是写了“(Collection)”而不是实际文本。
最佳答案
您应该逐行阅读文本文件并将它们添加到列表中。在它之后你可以通过你的列表设置你的ListBox
DataSource
。尝试:
const string f = "TextFile1.txt";
// 1
// Declare new List.
List<string> lines = new List<string>();
// 2
// Use using StreamReader for disposing.
using (StreamReader r = new StreamReader(f))
{
// 3
// Use while != null pattern for loop
string line;
while ((line = r.ReadLine()) != null)
{
// 4
// Insert logic here.
// ...
// "line" is a line in the file. Add it to our List.
lines.Add(line);
}
}
// 5
// Print out all the lines.
foreach (string s in lines)
{
Console.WriteLine(s);
}
关于C# 如何从 .txt 文件中的列表中读取并将它们放入列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31509483/