Added the storer implementations to the new project
This commit is contained in:
parent
b9df58509e
commit
0df278d861
21
SweetLib/Classes/Exceptions/RegistryStorerException.cs
Normal file
21
SweetLib/Classes/Exceptions/RegistryStorerException.cs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
using SweetLib.Classes.Storer;
|
||||||
|
|
||||||
|
namespace SweetLib.Classes.Exceptions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Exception thrown by <see cref="RegistryStorer"/>.
|
||||||
|
/// </summary>
|
||||||
|
public class RegistryStorerException : System.Exception
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="RegistryStorerException"/>.
|
||||||
|
/// </summary>
|
||||||
|
public RegistryStorerException() { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new <see cref="RegistryStorerException"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">Exception message.</param>
|
||||||
|
public RegistryStorerException(string message) : base(message) { }
|
||||||
|
}
|
||||||
|
}
|
80
SweetLib/Classes/Storer/IStorer.cs
Normal file
80
SweetLib/Classes/Storer/IStorer.cs
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
namespace SweetLib.Classes.Storer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Interface, which provides several methods to store simple data.
|
||||||
|
/// </summary>
|
||||||
|
public interface IStorer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a string value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||||
|
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||||
|
string ReadString(string section, string key, string defaultValue = "");
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads an integer value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||||
|
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||||
|
int ReadInteger(string section, string key, int defaultValue = 0);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads a bool value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="defaultValue">Default value, if this value does not exist.</param>
|
||||||
|
/// <returns>Value of the <see cref="key"/> in <see cref="section"/>.</returns>
|
||||||
|
bool ReadBool(string section, string key, bool defaultValue = false);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks, if a key exists inside a section.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <returns>True, if <see cref="key"/> is found inside <see cref="section"/>.</returns>
|
||||||
|
bool HasKey(string section, string key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a string value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="value">Value to be stored.</param>
|
||||||
|
void WriteString(string section, string key, string value);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes an integer value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="value">Value to be stored.</param>
|
||||||
|
void WriteInteger(string section, string key, int value);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Writes a bool value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key of the stored data.</param>
|
||||||
|
/// <param name="value">Value to be stored.</param>
|
||||||
|
void WriteBool(string section, string key, bool value);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a key inside a <see cref="section"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section in which the data is stored.</param>
|
||||||
|
/// <param name="key">Represents the key to be deleted.</param>
|
||||||
|
void DeleteKey(string section, string key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Deletes a section with all its keys.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="section">Represents the section to be deleted.</param>
|
||||||
|
void DeleteSection(string section);
|
||||||
|
}
|
||||||
|
}
|
82
SweetLib/Classes/Storer/IniFileStorer.cs
Normal file
82
SweetLib/Classes/Storer/IniFileStorer.cs
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
using System.Runtime.InteropServices;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace SweetLib.Classes.Storer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of an <see cref="IStorer"/> interface which stores the data inside an ini file.
|
||||||
|
/// </summary>
|
||||||
|
public class IniFileStorer : IStorer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Ini file path.
|
||||||
|
/// </summary>
|
||||||
|
public string FileName { get; }
|
||||||
|
|
||||||
|
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
||||||
|
private static extern long WritePrivateProfileString(string section, string key, string value, string filePath);
|
||||||
|
|
||||||
|
[DllImport("kernel32", CharSet = CharSet.Unicode)]
|
||||||
|
private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder value, int size, string filePath);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="IniFileStorer"/> with a specified file name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="fileName">The file name of the ini file.</param>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
94
SweetLib/Classes/Storer/RegistryStorer.cs
Normal file
94
SweetLib/Classes/Storer/RegistryStorer.cs
Normal file
|
@ -0,0 +1,94 @@
|
||||||
|
using Microsoft.Win32;
|
||||||
|
using SweetLib.Classes.Exception;
|
||||||
|
|
||||||
|
namespace SweetLib.Classes.Storer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Implementation of an <see cref="IStorer"/> interface which stores the data inside the registry.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Sections will be interpreted as subkeys on registry level.
|
||||||
|
/// </remarks>
|
||||||
|
public class RegistryStorer : IStorer
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The base registry key in which will be operated.
|
||||||
|
/// </summary>
|
||||||
|
public RegistryKey OperatingRegistryKey { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="RegistryStorer"/> with a specified application name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="appName">The applications base name. This will be used as name for a sub key inside the software key below the base key.</param>
|
||||||
|
/// <remarks>
|
||||||
|
/// This will use current user as the base key.
|
||||||
|
/// </remarks>
|
||||||
|
public RegistryStorer(string appName) : this(Registry.CurrentUser, appName) { }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates a new instance of <see cref="RegistryStorer"/> with a specified application name.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="baseRegistryKey">Provide a key of <see cref="Registry"/>, e.G. <i>Registry.CurrentUser</i>.</param>
|
||||||
|
/// <param name="appName">The applications base name. This will be used as name for a sub key inside the software key below the base key.</param>
|
||||||
|
public RegistryStorer(RegistryKey baseRegistryKey, string appName)
|
||||||
|
{
|
||||||
|
baseRegistryKey = baseRegistryKey.CreateSubKey("SOFTWARE");
|
||||||
|
OperatingRegistryKey = baseRegistryKey?.CreateSubKey(appName);
|
||||||
|
|
||||||
|
if (OperatingRegistryKey == null)
|
||||||
|
throw new RegistryStorerException("Unable to create registriy key.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ReadString(string section, string key, string defaultValue = "")
|
||||||
|
{
|
||||||
|
var localRegKey = OperatingRegistryKey.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 = OperatingRegistryKey.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 = OperatingRegistryKey.CreateSubKey(section);
|
||||||
|
localRegKey?.DeleteValue(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void DeleteSection(string section)
|
||||||
|
{
|
||||||
|
OperatingRegistryKey.DeleteSubKey(section);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue