How to save data on WPF?
i am trying to make a wpf save data after closed, then when i reopen the wpf, i will see the data i had the last time i opened the wpf. is there a way to do this?
14 Replies
exactly like you wrote it..
eg. on close save all relevant data and when you open the program load that data again
Save to a file
Preferably in %appdata%
Use a C# class to describe what data you want to save
Implement serialization of that class either to XML or json
Research ways an app can close
Determine when you want to save
If you're new to C#, I'd suggest using Newtonsoft.Json. It allows you to serialize an entire class with a single function call. From there, you can dump it into a file and reopen it, then deserialize it at runtime.
Example:
Isn't this also true for System.Text.Json?
If they have polymorphism or other complicated settings, custom serializers and etc are still necessary for newtonsoft
It is, but I use Newtonsoft more.
Either one works perfectly fine.
I recommend system text json, as it comes with .NET
No need to install a third party library
touche
no its the other way around use System.Text.Json, Json.NET should only be used in very specific scenarios where 1) u dont control the json you receive and it contains things STJ cannot handle and 2) for options STJ does not support currently which aren't that many.
On top of that the creators of STJ are the creators of Newtosoft that were hired by microsoft to create a better version of newtonsoft.
Personally, I've never cared. They both serialize and deserialize Json, and that's all I've ever needed.
TeBeCo
string json = Encoding.UTF8.GetString(buffer);
Quoted by
<@1102729783969861782> from #chat (click here)
React with ❌ to remove this embed.
Like using a bread knife and a chainsaw on sourdough interchangeably.
you will change your mind on why u should care in more reaons then u think
i got a solution, thanks