Hello,
i'll release my little config class for people who don't want to use the .net settings.
You will need the [Only registered and activated users can see links. Click Here To Register...] libary to run it or you will have to write your own (de-)serialization methods.
To load the config simply use:
Later in your code you can use for example:
To save your config use:
The benefetifs of this class is, that your variables have strong name and type declaration, standard values and you can simply add new variables.
Lg,
Shawak
i'll release my little config class for people who don't want to use the .net settings.
Code:
using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
class Config
{
// Config starts here
public static string Username = "Spieler";
public static int Port = 28282;
// End of config, don't change anything below
static BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static;
static FileInfo configFileInfo = new FileInfo(Constant.ConfigFile);
public static void Load()
{
// Check if file existts
if (!configFileInfo.Exists)
return;
// Deserialize from file
var json = File.ReadAllText(configFileInfo.FullName, Encoding.ASCII);
var jObject = JsonConvert.DeserializeObject<Config>(json);
// Create temp config
var tmpConf = new Dictionary<string, object>();
foreach (var field in jObject.GetType().GetFields(bindingFlags))
tmpConf.Add(field.Name, field.GetValue(null));
// Load values from temp config
foreach (var field in typeof(Config).GetFields(bindingFlags))
field.SetValue(field.Name, tmpConf[field.Name]);
}
public static void Save()
{
// Create configwith values
var tmpConf = new Dictionary<string, object>();
foreach (var v in typeof(Config).GetFields(bindingFlags))
tmpConf[v.Name] = v.GetValue(null);
// Serialize to file
var json = JsonConvert.SerializeObject(tmpConf, Formatting.Indented);
File.WriteAllText(configFileInfo.FullName, json);
}
}
To load the config simply use:
Code:
Config.Load();
Code:
Console.WriteLine(Config.Username); Config.Username = "Another Username"; Console.writeLine(Config.Username);
Code:
Config.Save();
Lg,
Shawak