Search code examples
c#object-persistence

C# Persisting Objects Between User Sessions Without using a database


I'm making a simple Winforms that reads in a text file and does some parsing to put it into C# objects that are then manipulated by my app.

I would like to parse the file once. Then go away for a week, come back and load all the objects back into my app.

I don't want to persist to database thus was hoping there was some sort of simple object persistance framework I could hook into.


Solution

  • You are talking about serialization.

    Look into the DataContractSerializer for this. If you want a file that isn't XML you can try the DataContractJsonSerializer.

    You simply add the DataContract and DataMember attributes to tell the serializer how to manage the serialization.

    If your objects will not change (new/old/renamed properties and visibility) and you don't care to read the serialization format, you can try the BinaryFormatter.


    There are several older serializers in the frame work (XmlSerializer, BinaryFormatter and more), but the best control over the serialization format will be with the above).