126 lines
3.5 KiB
C#
126 lines
3.5 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Text;
|
|
using VAR.ScreenAutomation.Interfaces;
|
|
|
|
namespace VAR.ScreenAutomation.Code
|
|
{
|
|
public class FileBackedConfiguration : IConfiguration
|
|
{
|
|
private MemoryBackedConfiguration _config = new MemoryBackedConfiguration();
|
|
|
|
private string _name = null;
|
|
|
|
public FileBackedConfiguration(string name = null)
|
|
{
|
|
_name = name;
|
|
}
|
|
|
|
private static string GetConfigFileName(string name = null)
|
|
{
|
|
string location = System.Reflection.Assembly.GetEntryAssembly().Location;
|
|
string path = Path.GetDirectoryName(location);
|
|
string filenameWithoutExtension = Path.GetFileNameWithoutExtension(location);
|
|
string configFile;
|
|
if (string.IsNullOrEmpty(name))
|
|
{
|
|
configFile = string.Format("{0}/{1}.cfg", path, filenameWithoutExtension);
|
|
}
|
|
else
|
|
{
|
|
configFile = string.Format("{0}/{1}_{2}.cfg", path, filenameWithoutExtension, name);
|
|
}
|
|
return configFile;
|
|
}
|
|
|
|
private static string[] GetConfigurationLines(string name = null)
|
|
{
|
|
string configFile = GetConfigFileName(name);
|
|
string[] config;
|
|
if (File.Exists(configFile) == false)
|
|
{
|
|
config = new string[0];
|
|
}
|
|
else
|
|
{
|
|
config = File.ReadAllLines(configFile);
|
|
}
|
|
return config;
|
|
}
|
|
|
|
public void Load(IConfiguration other = null)
|
|
{
|
|
_config.Clear();
|
|
if (other != null)
|
|
{
|
|
foreach (string key in other.GetKeys())
|
|
{
|
|
_config.Set(key, other.Get(key, null));
|
|
}
|
|
}
|
|
string[] configLines = GetConfigurationLines(_name);
|
|
foreach (string configLine in configLines)
|
|
{
|
|
int idxSplit = configLine.IndexOf('|');
|
|
if (idxSplit < 0) { continue; }
|
|
string configName = configLine.Substring(0, idxSplit);
|
|
string configData = configLine.Substring(idxSplit + 1);
|
|
|
|
_config.Set(configName, configData);
|
|
}
|
|
}
|
|
|
|
public void Save()
|
|
{
|
|
StringBuilder sbConfig = new StringBuilder();
|
|
foreach (string key in _config.GetKeys())
|
|
{
|
|
sbConfig.AppendFormat("{0}|{1}\n", key, _config.Get(key, string.Empty));
|
|
}
|
|
string configFileName = GetConfigFileName(_name);
|
|
File.WriteAllText(configFileName, sbConfig.ToString());
|
|
}
|
|
|
|
public IEnumerable<string> GetKeys()
|
|
{
|
|
return _config.GetKeys();
|
|
}
|
|
|
|
public void Clear()
|
|
{
|
|
_config.Clear();
|
|
}
|
|
|
|
public string Get(string key, string defaultValue)
|
|
{
|
|
return _config.Get(key, defaultValue);
|
|
}
|
|
|
|
public int Get(string key, int defaultValue)
|
|
{
|
|
return _config.Get(key, defaultValue);
|
|
}
|
|
|
|
public bool Get(string key, bool defaultValue)
|
|
{
|
|
return _config.Get(key, defaultValue);
|
|
}
|
|
|
|
public void Set(string key, string value)
|
|
{
|
|
_config.Set(key, value);
|
|
}
|
|
|
|
public void Set(string key, int value)
|
|
{
|
|
_config.Set(key, value);
|
|
}
|
|
|
|
public void Set(string key, bool value)
|
|
{
|
|
_config.Set(key, value);
|
|
}
|
|
|
|
}
|
|
}
|