Hade sparat variablerna i en klass och sedan helt enkelt serialize:a den med JSON.NET. Sjukt lätt.
Här har jag slängt ihop ett exempel där jag har en klass Settings som har tre properties "name, xp och health" som jag spar till fil och kan läsa från fil.
class Settings
{
public int health { get; set; }
public int xp { get; set; }
public string name { get; set; }
public Settings(int h, int xp, string n)
{
this.health = h;
this.xp = xp;
this.name = n;
}
public Settings(string filename)
{
if (!File.Exists(filename))
throw new Exception("File not found");
string text = File.ReadAllText(filename);
if (string.IsNullOrEmpty(text))
throw new Exception("File was either blank or had errors");
Settings temp = JsonConvert.DeserializeObject<Settings>(text);
this.name = temp.name;
this.xp = temp.xp;
this.health = temp.health;
}
public Settings() { }
public void SaveToFile(string filename)
{
string serialized = JsonConvert.SerializeObject(this);
File.WriteAllText(filename, serialized);
}
}
Och main:
static void Main(string[] args)
{
Settings s = new Settings(1, 1, "asd");
s.SaveToFile(@C:\Users\David\Desktop\asd.txt); // spar till fil
Settings k = new Settings(@C:\Users\David\Desktop\asd.txt); // läs från fil
Console.WriteLine(k.name); // skriv ut namn
Console.ReadLine();
}
Kolla bara efter Newtonsoft.JSON så hittar du det.