+ 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.
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{}
+ 3
Try writing your variables on a file and then retrieving them.
Use StreamReader and StreamWriter classes.