Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple config class
public static class SimpleConfig
{
static string configFilePath;
public static void InitializeInLocalApplicationData(string name)
{
Initialize(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
name,
"config.xml"
));
}
public static void InitializeInApplicationData(string name)
{
Initialize(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
name,
"config.xml"
));
}
public static void InitializeInCommonApplicationData(string name)
{
Initialize(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
name,
"config.xml"
));
}
public static void Initialize(string configPath)
{
configFilePath = configPath;
Directory.CreateDirectory(Path.GetDirectoryName(configFilePath));
if (!File.Exists(configFilePath))
{
File.WriteAllText(configFilePath, @"<config />");
}
}
public static void OpenConfig()
{
Process.Start(configFilePath);
}
public static string GetValue(string key, string defaultValue = "")
{
var config = XDocument.Load(configFilePath).Root;
var node = config.Descendants(key).FirstOrDefault();
if (node == null)
{
return defaultValue;
}
return node.Value;
}
public static void SetValue(string key, string value)
{
var config = XDocument.Load(configFilePath).Root;
var node = config.Descendants(key).FirstOrDefault();
if (node == null)
{
node = new XElement(key);
config.Add(node);
}
node.Value = value;
config.Save(configFilePath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.