❔ How can I make it so an array saves changes when I turn off the program
Basically I'm making an array of strings that can be removed or added to - how can I make it so if say I add the string "Dog" to it, when I come back to the code it will still have "Dog" in it without me having to re-add it. I'm in college so I have very little knowledge of C# but I am trying to learn. I use Visual Studio Community. Thanks
12 Replies
There is a Concept called "serialization" that will help you do this
You need to store it to disk, IE permanent storage.
You can do the serialization any way you'd like, I personally prefer to store it as binary.
You could serialize your array/list to a string, then write that string to a file
Your program can then try and read the file when it starts and deserialize the content to an array/list again
I'm curious, what would be your suggested method for saving a string list as binary?
Depends, in an insecure way by this.
Right, so manually making your own binary writer/reader
Pretty much
Sure
Since it's just strings and not custom classes, serializing and deserializing is easy. No need for an external library or anything.
Yep, that's fine 🙂
The added data is just to know how much data to retrieve, it's type, and version is for compatibility, while the magic bytes is to check if the file is valid and not just a file with renamed extension
Thank you both I'll look into it now
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.