开发者

Save data of a two-column listbox to file and load it

I am working on a list program that contains a 2-column listbox. Both columns contain strings that I wish to save as a file. The file format doesn开发者_Go百科't really matter as long as it can be read and populate the listbox again at startup.

Any help will be extremely appreciated!


You could populate your list box from a custom object and serialize that object to disk:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Windows.Forms;


//Serialize an object to disk (properties must be public):

public string Serialize(Object Input, string OutFile)
{
    System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

    XmlSerializer XML = new XmlSerializer(Input.GetType());
    XML.Serialize(memoryStream, Input);

    memoryStream.Flush();
    memoryStream.Position = 0;

    if (!string.IsNullOrEmpty(OutFile))
    {
        using (FileStream fileStream = new FileStream(OutFile, FileMode.Create))
        {
            byte[] data = memoryStream.ToArray();
            fileStream.Write(data, 0, data.Length);
            fileStream.Close();
        }
    }

    return new System.IO.StreamReader(memoryStream).ReadToEnd();
}


//Deserialize from a serialized file:

    public object DeserializeFile(Type ObjectType, string FileName)
{
    Type type = typeof(object);

    if (ObjectType != null)
    { type = ObjectType; }

    using (FileStream fileStream = new FileStream(FileName, FileMode.Open, FileAccess.Read))
    {
        StreamReader sr = new StreamReader(fileStream);
        string XML = sr.ReadToEnd();

        XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(type);
        fileStream.Close();

        return xmlSerializer.Deserialize(new StringReader(XML));
    }
}

CustomObject co = DeserializeFile(typeof(CustomObject), fileName.xml) as CustomObject;

I use this method to save the contents of a CheckedListBoxControl to disk so users are able to define it with their own items.


First off, the display of your data and what gui widgets you use is completely irrelevant (i.e. that you are using a "2 column listbox" doesn't matter). What is relevant (in this case of a listbox) is the collection of objects that is bound to your listbox.

The collection class is really the important element here for the purposes of loading & saving. Its contents are what you will be saving and loading from your file. The listbox that displays the data doesn't care; it simply displays the data of the collection it is bound to using whatever template you've setup.

So how do you load & save you ask? Here's a simple example that uses binary formatting. There are a gazillion other walkthrus on the Intertubes; just google "C# Serialization to file"

   public void Serialize(string filename, ObjectToSerialize objectToSerialize)
    {
       Stream stream = File.Open(filename, FileMode.Create);
       BinaryFormatter bFormatter = new BinaryFormatter();
       bFormatter.Serialize(stream, objectToSerialize);
       stream.Close();
    }

   public ObjectToSerialize DeSerialize(string filename)
    {
       ObjectToSerialize objectToSerialize;
       Stream stream = File.Open(filename, FileMode.Open);
       BinaryFormatter bFormatter = new BinaryFormatter();
       objectToSerialize = (ObjectToSerialize)bFormatter.Deserialize(stream);
       stream.Close();
       return objectToSerialize;
    }


Look into the StreamReader and StreamWriter classes.

For saving the listbox contents:

   using (StreamWriter tWrite = new StreamWriter(@"c:\temp\test.txt"))
    {
      foreach (string tItem in listBox1.Items)
      {
        tWrite.WriteLine(tItem);
      }
    }

For reading file contents:

listBox1.Items.AddRange(File.ReadAllLines(@"c:\temp\test.txt"));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜