Reading Data from Text File to a CheckedListBox
Hallo There was hopng I could ask for some advice. Which would be 开发者_如何学运维the best way to read data from a textFile and adding this to a checkedListBox?
something like this although this doesn't work out properly.
FileStream fs = new FileStream("../../Features.txt", FileMode.Open, FileAccess.Read);
BufferedStream bs = new BufferedStream(fs);
fs.Close();
StreamReader sr = new StreamReader("../../Features.txt");
chkFeatures.Items.Add(sr.ReadToEnd());
sr.Close();
Regards Arian
Here is another way:
string filePath = @"C:\test.txt";
if (System.IO.File.Exists(filePath))
checkedListBox1.Items.AddRange(System.IO.File.ReadAllLines(filePath));
Try the following:
using (StreamReader sr = new StreamReader("../../Features.txt"))
{
while (sr.Peek() >= 0)
{
chkFeatures.Items.Add(sr.ReadLine());
}
}
Refer to:
StreamReader.ReadLine Method
StreamReader Class
精彩评论