diff --git a/SweetLib/Classes/Exception/RegistryStorerException.cs b/SweetLib/Classes/Exception/RegistryStorerException.cs new file mode 100644 index 0000000..c2cf14c --- /dev/null +++ b/SweetLib/Classes/Exception/RegistryStorerException.cs @@ -0,0 +1,9 @@ +namespace SweetLib.Classes.Exception +{ + public class RegistryStorerException : System.Exception + { + public RegistryStorerException(){} + + public RegistryStorerException(string message):base(message) {} + } +} diff --git a/SweetLib/Classes/Storer/IStorer.cs b/SweetLib/Classes/Storer/IStorer.cs new file mode 100644 index 0000000..f1c0505 --- /dev/null +++ b/SweetLib/Classes/Storer/IStorer.cs @@ -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); + } +} diff --git a/SweetLib/Classes/Storer/IniFileStorer.cs b/SweetLib/Classes/Storer/IniFileStorer.cs new file mode 100644 index 0000000..f913813 --- /dev/null +++ b/SweetLib/Classes/Storer/IniFileStorer.cs @@ -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); + } + } +} diff --git a/SweetLib/Classes/Storer/RegistryStorer.cs b/SweetLib/Classes/Storer/RegistryStorer.cs new file mode 100644 index 0000000..8803232 --- /dev/null +++ b/SweetLib/Classes/Storer/RegistryStorer.cs @@ -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); + } + } +} diff --git a/SweetLib/SweetLib.csproj b/SweetLib/SweetLib.csproj index 2970c8f..f6c11d4 100644 --- a/SweetLib/SweetLib.csproj +++ b/SweetLib/SweetLib.csproj @@ -41,6 +41,10 @@ + + + +