user850010
user850010

Reputation: 6359

How to save current application state of classes using Winform?

Until now, I only had to store application state of primitive types like int, string, boolean and for that I used Properties.Settings.Default object with no problem.

But now I also need to store instances of classes created with new keyword.

So my question is how to store these types of objects?

Upvotes: 2

Views: 750

Answers (2)

boj
boj

Reputation: 11395

Serialize your object with XmlSerializer in that way:

var p=new Person();
p.FirstName = "Jeff";
p.MI = "A";
p.LastName = "Price";
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(p.GetType());
x.Serialize(Console.Out, p);

Source and full example: http://support.microsoft.com/kb/815813

For the complete process - serializing and deserializing - visit this link:

http://sharpertutorials.com/serialization/

Notes:

  • you have to use the attribute [Serializable] (or other, it depends on the serializer) on your class
  • try using POCO objects
  • depending on your .NET version, consider toughts in this Codeproject article.

Upvotes: 2

Tigran
Tigran

Reputation: 62286

You can use msdn: ObjectSerialization.

You have to read a couple of articles in order to select right serialization mechanism for your objects.

Regards.

Upvotes: 1

Related Questions