开发者

Reading Data from Text File to a CheckedListBox

开发者 https://www.devze.com 2023-03-07 19:31 出处:网络
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?

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号