【问题标题】:Reading in data from txt file and Adding it to an Observable Collection从 txt 文件中读取数据并将其添加到 Observable 集合中
【发布时间】:2013-09-16 20:36:52
【问题描述】:

我正在尝试构建一个简单的保存和加载程序,它从文本文件中读取用户,将其添加到可观察的集合中,然后用可观察的用户集合填充列表框。

虽然我了解如何将用户添加到我的可观察集合,然后将该集合写入 txt 文件,但我对如何读取用户并将他重新添加到 ObservableCollection 感到困惑用户。

我的用户类:

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

问题方法:

 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();
        }

有什么想法吗?

【问题讨论】:

    标签: c# file-io load save observablecollection


    【解决方案1】:

    您应该研究序列化,而不是为此编写文本文件。

    看看这个:http://tanguay.info/web2010/index.php?pg=codeExamples&id=224

    【讨论】:

    • 这很有帮助。比我做的简单得多。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多