将两个列表保存到文件中,然后加载c#

时间:2012-08-30 08:24:14

标签: c#

在我的班级“DiaryManager”中,我有两个不同类型的列表(T),我想将其保存到文件,然后我想加载它。 我得到它与我的一个列表一起工作,因为我要告诉你。

我保存并加载到我的工作代码中的列表名为“m_diary”。保存方法是这样的:

    /// <summary>
    /// Saves the object information
    /// </summary>
    public void Save()
    {
        // Gain code access to the file that we are going
        // to write to
        try
        {
            // Create a FileStream that will write data to file.
            FileStream writerFileStream =
                new FileStream(DATA_FILENAME, FileMode.Create, FileAccess.Write);
            // Save our dictionary of friends to file
            m_formatter.Serialize(writerFileStream, m_diary);

            // Close the writerFileStream when we are done.
            writerFileStream.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        } 
    }

我的加载方法就是这个:

    /// <summary>
    /// Load the object to the program 
    /// </summary>
    public void Load()
    {

        // Check if we had previously Save information of our friends
        // previously
        if (File.Exists(DATA_FILENAME))
        {

            try
            {
                // Create a FileStream will gain read access to the
                // data file.
                FileStream readerFileStream = new FileStream(DATA_FILENAME,
                    FileMode.Open, FileAccess.Read);
                // Reconstruct information of our friends from file.
                m_diary = (List<Diary>)                    
                    m_formatter.Deserialize(readerFileStream);
                // Close the readerFileStream when we are done
                readerFileStream.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 

        } 

    }

“DATA_FILENAME”是这个常数:

    private const string DATA_FILENAME = "TrainingDiary.dat";

此代码在我的Windows窗体类中运行良好。 但是现在Iv又增加了一个不同类型的列表。

如何保存并加载第二个列表? :)

祝你好运 Cyrix的

2 个答案:

答案 0 :(得分:1)

您应该创建一个包含要保存的所有数据(列表)的类。然后将该类保存到文件中。

答案 1 :(得分:1)

你可以使用第二个列表的类似代码来做,或者你可以写一个generic method

   public static void Save<T>(string fileName, List<T> list)
    {
        // Gain code access to the file that we are going
        // to write to
        try
        {
            // Create a FileStream that will write data to file.
            using (var stream = new FileStream(fileName, FileMode.Create, FileAccess.Write))
            {
                var formatter = new BinaryFormatter();
                 formatter.Serialize(stream, list);
            }

        }
        catch (Exception ex)
        {
           Console.WriteLine(ex.Message);
        }
    }

加载方法:

   public static List<T> Load<T>(string fileName)
    {
        var list = new List<T>();
        // Check if we had previously Save information of our friends
        // previously
        if (File.Exists(fileName))
        {

            try
            {
                // Create a FileStream will gain read access to the
                // data file.
                using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    var formatter = new BinaryFormatter();
                    list = (List<T>)
                        formatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
        return list;
    }

负载的使用:

        var list = new List<string> {"one", "two", "three"};
        Save("first.dat", list);

        var list2 = Load<string>("first.dat");
        foreach (var VARIABLE in list2)
        {
            Console.WriteLine(VARIABLE);
        }

另请参阅using Statement以处理open/close streams;