Added Storer interface and ini storer and registry storer implementations

This commit is contained in:
Serraniel 2017-04-12 18:49:58 +02:00
parent dd5fc290b2
commit 7fdc1a0ad4
5 changed files with 181 additions and 0 deletions

View file

@ -0,0 +1,9 @@
namespace SweetLib.Classes.Exception
{
public class RegistryStorerException : System.Exception
{
public RegistryStorerException(){}
public RegistryStorerException(string message):base(message) {}
}
}

View file

@ -0,0 +1,23 @@
namespace SweetLib.Classes.Storer
{
public interface IStorer
{
string ReadString(string section, string key, string defaultValue = "");
int ReadInteger(string section, string key, int defaultValue = 0);
bool ReadBool(string section, string key, bool defaultValue = false);
bool HasKey(string section, string key);
void WriteString(string section, string key, string value);
void WriteInteger(string section, string key, int value);
void WriteBool(string section, string key, bool value);
void DeleteKey(string section, string key);
void DeleteSection(string section);
}
}

View file

@ -0,0 +1,72 @@
using System.Runtime.InteropServices;
using System.Text;
namespace SweetLib.Classes.Storer
{
public class IniFileStorer : IStorer
{
public string FileName { get; }
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);
[DllImport("kernel32", CharSet = CharSet.Unicode)]
static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);
public IniFileStorer(string fileName)
{
FileName = fileName;
}
public string ReadString(string section, string key, string defaultValue = "")
{
var builder = new StringBuilder(255);
GetPrivateProfileString(section, key, defaultValue, builder, 255, FileName);
return builder.ToString();
}
public int ReadInteger(string section, string key, int defaultValue = 0)
{
int result;
if (!int.TryParse(ReadString(section, key, defaultValue.ToString()), out result))
result = defaultValue;
return result;
}
public bool ReadBool(string section, string key, bool defaultValue = false)
{
return ReadInteger(section, key, defaultValue ? 1 : 0) > 0;
}
public bool HasKey(string section, string key)
{
return ReadString(section, key).Length > 0;
}
public void WriteString(string section, string key, string value)
{
WritePrivateProfileString(section, key, value, FileName);
}
public void WriteInteger(string section, string key, int value)
{
WriteString(section, key, value.ToString());
}
public void WriteBool(string section, string key, bool value)
{
WriteInteger(section, key, value ? 1 : 0);
}
public void DeleteKey(string section, string key)
{
WriteString(section, key, null);
}
public void DeleteSection(string section)
{
WriteString(section, null, null);
}
}
}

View file

@ -0,0 +1,73 @@
using Microsoft.Win32;
using SweetLib.Classes.Exception;
namespace SweetLib.Classes.Storer
{
public class RegistryStorer : IStorer
{
public RegistryKey BaseRegistryKey { get; }
public RegistryStorer(string appName) : this(Registry.CurrentUser, appName) { }
public RegistryStorer(RegistryKey baseRegistryKey, string appName)
{
baseRegistryKey = baseRegistryKey.CreateSubKey("SOFTWARE");
BaseRegistryKey = baseRegistryKey?.CreateSubKey(appName);
if (BaseRegistryKey == null)
throw new RegistryStorerException("Unable to create registriy key.");
}
public string ReadString(string section, string key, string defaultValue = "")
{
var localRegKey = BaseRegistryKey.OpenSubKey(section);
return (string)localRegKey?.GetValue(key.ToUpper());
}
public int ReadInteger(string section, string key, int defaultValue = 0)
{
int result;
if (!int.TryParse(ReadString(section, key, defaultValue.ToString()), out result))
result = defaultValue;
return result;
}
public bool ReadBool(string section, string key, bool defaultValue = false)
{
return ReadInteger(section, key, defaultValue ? 1 : 0) > 0;
}
public bool HasKey(string section, string key)
{
return ReadString(section, key).Length > 0;
}
public void WriteString(string section, string key, string value)
{
var localRegKey = BaseRegistryKey.CreateSubKey(section);
localRegKey?.SetValue(key.ToUpper(), value);
}
public void WriteInteger(string section, string key, int value)
{
WriteString(section, key, value.ToString());
}
public void WriteBool(string section, string key, bool value)
{
WriteInteger(section, key, value ? 1 : 0);
}
public void DeleteKey(string section, string key)
{
var localRegKey = BaseRegistryKey.CreateSubKey(section);
localRegKey?.DeleteValue(key);
}
public void DeleteSection(string section)
{
BaseRegistryKey.DeleteSubKey(section);
}
}
}

View file

@ -41,6 +41,10 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Classes\Exception\RegistryStorerException.cs" />
<Compile Include="Classes\Storer\IniFileStorer.cs" />
<Compile Include="Classes\Storer\IStorer.cs" />
<Compile Include="Classes\Storer\RegistryStorer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\Logger\Memory\ArchivableConsoleLogMemory.cs" />
<Compile Include="Utils\Logger\Memory\ILogMemory.cs" />