How can I do that my variables save in memory? WinForms C# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

13th Mar 2018, 10:25 PM
SebGM2018
SebGM2018 - avatar
2 Answers
+ 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{}
13th Mar 2018, 11:10 PM
Vladi Petrov
Vladi Petrov - avatar
+ 3
Try writing your variables on a file and then retrieving them. Use StreamReader and StreamWriter classes.
13th Mar 2018, 11:12 PM
RiadhTN
RiadhTN - avatar