128 lines
3.8 KiB
C#
128 lines
3.8 KiB
C#
//string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
// Store config in application directory
|
|
// string appData = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
|
|
namespace UPSNet
|
|
{
|
|
public static class AppConfig
|
|
{
|
|
private static readonly string ConfigFilePath;
|
|
private static Dictionary<string, string> _settings;
|
|
|
|
public static bool exists = false;
|
|
|
|
static AppConfig() {
|
|
// Store config in application directory
|
|
ConfigFilePath = Path.Combine(
|
|
AppDomain.CurrentDomain.BaseDirectory,
|
|
"config.json");
|
|
|
|
_settings = new Dictionary<string, string>();
|
|
exists = LoadSettings();
|
|
}
|
|
|
|
public static string Get(string key, string defaultValue = "")
|
|
{
|
|
return _settings.ContainsKey(key) ? _settings[key] : defaultValue;
|
|
}
|
|
|
|
public static int GetInt(string key, int defaultValue = 0)
|
|
{
|
|
int result;
|
|
return _settings.ContainsKey(key) && int.TryParse(_settings[key], out result)
|
|
? result
|
|
: defaultValue;
|
|
}
|
|
|
|
public static bool GetBool(string key, bool defaultValue = false)
|
|
{
|
|
bool result;
|
|
return _settings.ContainsKey(key) && bool.TryParse(_settings[key], out result)
|
|
? result
|
|
: defaultValue;
|
|
}
|
|
|
|
public static void Set(string key, string value)
|
|
{
|
|
_settings[key] = value;
|
|
//SaveSettings();
|
|
}
|
|
|
|
public static void Set(string key, int value)
|
|
{
|
|
_settings[key] = value.ToString();
|
|
//SaveSettings();
|
|
}
|
|
|
|
public static void Set(string key, bool value)
|
|
{
|
|
_settings[key] = value.ToString().ToLower();
|
|
//SaveSettings();
|
|
}
|
|
|
|
public static bool LoadSettings()
|
|
{
|
|
if (!File.Exists(ConfigFilePath)) return false;
|
|
|
|
try
|
|
{
|
|
string json = File.ReadAllText(ConfigFilePath);
|
|
json = json.Trim('{', '}', ' ', '\t', '\n', '\r');
|
|
|
|
string[] pairs = json.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
foreach (string pair in pairs)
|
|
{
|
|
string[] parts = pair.Split(new[] { ':' }, 2);
|
|
if (parts.Length == 2)
|
|
{
|
|
string key = parts[0].Trim().Trim('"');
|
|
string value = parts[1].Trim().Trim('"');
|
|
_settings[key] = value;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
catch
|
|
{
|
|
// Reset if loading fails
|
|
_settings = new Dictionary<string, string>();
|
|
return false;
|
|
}
|
|
//return false;
|
|
}
|
|
|
|
public static void SaveSettings()
|
|
{
|
|
try
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.Append("{");
|
|
|
|
bool first = true;
|
|
foreach (KeyValuePair<string, string> kvp in _settings)
|
|
{
|
|
if (!first) sb.Append(",");
|
|
first = false;
|
|
|
|
sb.Append("\"");
|
|
sb.Append(kvp.Key);
|
|
sb.Append("\":\"");
|
|
sb.Append(kvp.Value);
|
|
sb.Append("\"");
|
|
}
|
|
|
|
sb.Append("}");
|
|
File.WriteAllText(ConfigFilePath, sb.ToString());
|
|
}
|
|
catch
|
|
{
|
|
// Ignore save errors
|
|
}
|
|
}
|
|
}
|
|
} |