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.
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).
You could use application settings or user settings. Its pretty easy with the wizard that can be found under the projects properties. here is the tutorial for it.
You could just serialize them to an XML file. Simple, quick and easy (unless someone hacks the file).
The simplest non-DB method of storing object state is serialization. Mark your class as either Serializable or a DataContract, then use a BinaryFormatter (or DataContractSerializer if you used the DataContract) to convert the object into a persistable state. That can then be saved to a file that you can load when the app starts back up and deserialize in a similar fashion. Be aware that the class's members, including child classes, must be serializable in the same way, and that you will lose any references to non-serialized elements such as pointers and delegate references.
You can use serialization as sugested before.
A good place to save the data in is Windows's LocalData folder -
Environment.SpecialFolder.LocalApplicationData
Probably the simplest way to do this would be to serialize the objects directly to a file, using the BinaryFormatter
class. You'll need to make sure that all your types are marked as [Serializable]
but this approach is fast and easy.
Here is a simple example of a "read" and "write" method for an arbitrary object graph:
private void SerializeToFile(string fileName, object o)
{
var binaryFormatter = new BinaryFormatter();
using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None))
{
binaryFormatter.Serialize(fileStream, o);
}
}
private object DeserializeFromFile(string fileName)
{
var binaryFormatter = new BinaryFormatter();
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
return binaryFormatter.Deserialize(fileStream);
}
}
Other approaches include XmlSerialization (the XmlSerializer
class) - better if you want to the serialized objects to be human readable or editable, or using the newer DataContractSerializer
.
What is wrong with a database ? If you do not want to have a full engine with its setup and associated problems, maybe you can take a look at "file" based databases...
My first toughs are :
- SqlCompact
- Access databases
- SqlLite
...
Depending the complexity of your objects to persist, it can be a good solution.
精彩评论