C
C#10mo ago
inactive

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
ACiDCA7
ACiDCA710mo ago
exactly like you wrote it.. eg. on close save all relevant data and when you open the program load that data again
Denis
Denis10mo ago
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
oke
oke10mo ago
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:
[Serializable]
internal class MyAppSettings
{
public string CurrentUser { get; set; } = string.Empty;
public int UserAge { get; set; } = -1;
public List<string> GreatestFears { get; set; } = new();
}

...

public static void Main()
{
// Save an instance of the class to file
var settings = new MyAppSettings();
string jsonString = JsonConvert.SerializeObject(settings);
File.WriteAllText("path/to/settings/file", jsonString);

// Load file
var loadedJson = File.ReadAllText("path/to/settings/file");
var newSettings = JsonConvert.DeserializeObject<MyAppSettings>(loadedJson);
// Now you can use the loaded class in your app
}
[Serializable]
internal class MyAppSettings
{
public string CurrentUser { get; set; } = string.Empty;
public int UserAge { get; set; } = -1;
public List<string> GreatestFears { get; set; } = new();
}

...

public static void Main()
{
// Save an instance of the class to file
var settings = new MyAppSettings();
string jsonString = JsonConvert.SerializeObject(settings);
File.WriteAllText("path/to/settings/file", jsonString);

// Load file
var loadedJson = File.ReadAllText("path/to/settings/file");
var newSettings = JsonConvert.DeserializeObject<MyAppSettings>(loadedJson);
// Now you can use the loaded class in your app
}
Denis
Denis10mo ago
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
oke
oke10mo ago
It is, but I use Newtonsoft more. Either one works perfectly fine.
Denis
Denis10mo ago
I recommend system text json, as it comes with .NET No need to install a third party library
oke
oke10mo ago
touche
leowest
leowest10mo ago
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.
oke
oke10mo ago
Personally, I've never cared. They both serialize and deserialize Json, and that's all I've ever needed.
MODiX
MODiX10mo ago
TeBeCo
string json = Encoding.UTF8.GetString(buffer);
Quoted by
<@1102729783969861782> from #chat (click here)
React with ❌ to remove this embed.
oke
oke10mo ago
Like using a bread knife and a chainsaw on sourdough interchangeably.
leowest
leowest10mo ago
you will change your mind on why u should care in more reaons then u think
inactive
inactiveOP10mo ago
i got a solution, thanks
Want results from more Discord servers?
Add your server