简体   繁体   English

从txt文件中读取数据并将其添加到可观察的集合中

[英]Reading in data from txt file and Adding it to an Observable Collection

I'm attempting to build a simple save-and-load program that reads Users in from a text file, adds it to an observable collection, and then populates a listbox with the observable collection of Users. 我试图构建一个简单的保存和加载程序,该程序从文本文件中读取用户,将其添加到可观察的集合中,然后使用可观察的用户集合填充列表框。

While I understand how to add Users to my observable collection and then write that collection to the the txt file, I am confused as to how I am supposed to read-in a user and re-add him to the the ObservableCollection of users. 虽然我了解了如何将“用户”添加到我的可观察集合中,然后将该集合写入txt文件,但我对应该如何读入用户并将其重新添加到用户的“观察对象集合”感到困惑。

my user class: 我的用户类别:

public class User
{
    public string UserName { get; set; }
    public string Password { get; set; }
}

Problem method: 问题方法:

 ObservableCollection<User> LoadActionList = new ObservableCollection<User>();
        using (System.IO.StreamReader file = new System.IO.StreamReader("SavedAccounts.txt"))
        {
            string line;
            while (!file.EndOfStream)
            {
                line = file.ReadLine();
                //LoadActionList.Add(line); //how to detect that a line is a User?
            }
            file.Close();
        }

Any ideas? 有任何想法吗?

Instead of writing text files for this you should look into serialization. 与其为此编写文本文件,不如考虑序列化。

Check this out: http://tanguay.info/web2010/index.php?pg=codeExamples&id=224 检查一下: http : //tanguay.info/web2010/index.php?pg=codeExamples&id=224

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM