I have settings.cs file and settings.settings file. Settings.cs is longer, so I think this is needed. Hm... what should I do?
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreLib.Io;
using CoreLib;
using StreamWriter = System.IO.StreamWriter;
namespace MapViewer
{
public static class Settings
{
#region Basic
private static Dictionary<string, string> data = new Dictionary<string, string>();
private const string SettingsPath = "settings.txt";
static Settings()
{
if (File.Exists(SettingsPath))
{
List<string> vals = File.ReadAndCleanLines(SettingsPath);
foreach (string s in vals)
{
try
{
string[] parts = s.Split('=');
if (parts.Length != 2)
continue;
parts[0].ChangeTrim();
parts[1].ChangeTrim();
data.Add(parts[0], parts[1]);
}
catch
{
//A key was invalid, but we don't care, we'll just skip it.
}
}
}
}
public static string GetValue(string key)
{
string ret;
if (!data.TryGetValue(key, out ret))
return string.Empty;
return ret;
}
public static string TryGetValue(string key)
{
string ret;
if (!data.TryGetValue(key, out ret))
return null;
return ret;
}
public static void SetValue(string key, string value)
{
data[key] = value;
Save();
}
private static void Save()
{
using (StreamWriter sw = new StreamWriter("settings.txt", false))
{
foreach (KeyValuePair<string, string> item in data)
{
sw.Write(item.Key);
sw.Write('=');
sw.Write(item.Value);
sw.WriteLine();
}
}
}
#endregion
#region File Paths
private const string GamePathKey = "GamePath";
public static string GamePath
{
get
{
string val = TryGetValue(GamePathKey);
if (val == null)
SetValue(GamePathKey, string.Empty);
return val;
}
set
{
SetValue(GamePathKey, value);
}
}
#endregion
}
}