How can I do that my variables save in memory? WinForms C# | SoloLearn: Learn to code for FREE!

+5

How can I do that my variables save in memory? WinForms C#

I mean, i want that my variable saves in memory and i close the app and the value stills there.

3/13/2018 10:25:41 PM

SebGM2018

2 Answers

New Answer

+5

You can save variable to file. You shoud serialize variable and then deserialize. Code below can help you. public void SerializeObject<T>(string filename, T obj) { Stream stream = File.Open(filename, FileMode.Create); BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(stream, obj); stream.Close(); } public T DeSerializeObject<T> (string filename) { T objectToBeDeSerialized; Stream stream = File.Open(filename, FileMode.Open); BinaryFormatter binaryFormatter = new BinaryFormatter(); objectToBeDeSerialized= (T)binaryFormatter.Deserialize(stream); stream.Close(); return objectToBeDeSerialized; } P.S If you want to save object you should mark object as Serializable. For example [Serializable] class MyClass{}

+3

Try writing your variables on a file and then retrieving them. Use StreamReader and StreamWriter classes.