using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Diagnostics; public class Config { public static Config Instance { get; private set; } #region Proberty public static string Comments { get { return comments; } } private static string comments = string.Empty; private readonly Dictionary properties; private bool isInitialized; public static Random Random { get; private set; } public string ConnectIP { get; private set; } public string TunnelIP { get; private set; } public int TunnelPort { get; private set; } public int WorkInteval { get; private set; } //Database shit public string SQLHost { get; private set; } public string SQLUser { get; private set; } public string SQLPassword { get; private set; } public string SQLDatabase { get; private set; } #endregion; private Config() { properties = new Dictionary(); isInitialized = false; } public static bool Load() { try { Instance = new Config(); return Instance.InitializeInternal(); } catch (Exception ex) { Log.WriteLine(LogLevel.Exception, "Config exception: {0}", ex.ToString()); return false; } } private const string ConfigName = "\\Config.cfg"; private static readonly string configPath = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + ConfigName; /// /// Automatically loads settings from config file /// private bool InitializeInternal() { if (isInitialized) { return true; } try { ParseFile(configPath); //Tunnel ConnectIP = GetString("Tunnel.ConnectIP"); TunnelIP = GetString("Tunnel.IP"); TunnelPort = GetInt32("Tunnel.Port"); WorkInteval = GetInt32("Tunnel.WorkInteval"); //Database SQLHost = GetString("Sql.Server"); SQLUser = GetString("Sql.User"); SQLPassword = GetString("Sql.Password"); SQLDatabase = GetString("Sql.Database"); } catch (Exception ex) { Log.WriteLine(LogLevel.Exception, "Error reading Config: {0}", ex.Message); return false; } Random = new Random(DateTime.Now.Second); Log.WriteLine(LogLevel.Info, "Config loaded successfully."); isInitialized = true; return true; } #region Get methods /// /// Gets a Boolean from the file /// /// The key /// true if value is true, else returns false public bool GetBool(string key) { return GetString(key).ToLower() == "true"; } /// /// Gets an Int32 type variable from the file /// /// The key to get the value from /// 'key's Int32 value public int GetInt32(string key) { return Convert.ToInt32(properties[key]); } public uint GetUInt32(string key) { return Convert.ToUInt32(properties[key]); } /// /// Gets an Int16 type variable from the file /// /// The key to get the value from /// 'key's Int16 value public short GetInt16(string key) { return Convert.ToInt16(properties[key]); } /// /// Gets an Byte type variable from the file /// /// The key to get the value from /// 'key's Byte value public byte GetByte(string key) { return Convert.ToByte(properties[key]); } /// /// Gets a String type variable from the file /// /// The key to get the value from /// 'key's String vaule public string GetString(string key) { return properties[key].ToString(); } #endregion /// /// Reads the file and parse it into a List of Key Vaule Pairs. /// /// filepath /// List of Key Value Pairs public void ParseFile(string fileName) { string[] lines = File.ReadAllLines(fileName); foreach (string entry in lines.Select(line => line.Trim()).Where(line => line.Length > 0)) { if (!entry.Contains("#")) { string[] parts = entry.Split('='); if (parts.Length != 2) { continue; } string key = parts[0].Trim(); string value = parts[1].Trim(); if (!properties.ContainsKey(key)) { properties.Add(key, value); } } else { comments += Environment.NewLine + entry.Remove(0, 1); } } } }