|
using Microsoft.Win32;
using System;
namespace RegistryAccess
{
/// <summary>
/// Static class for reading and writing data from / to the Windows Registry.
/// </summary>
public static class RegistryHandler
{
/// <summary>
/// Registry-related application settings.
/// </summary>
private const string RegistryAppKey = "BookStore";
private const string MainSubKey = "Software";
private const string DbFolderPathString = "Database folder path";
private const string TempFolderPathString = "Temp folder path";
/// <summary>
/// Commits the settings to the Windows Registry.
/// </summary>
/// <param name="dbFolderPath">The folder containing the database file</param>
/// <param name="tempFolderPath">The application temporary folder</param>
public static void CommitSettingsToRegistry(string dbFolderPath, string tempFolderPath)
{
try
{
RegistryKey mainKey = Registry.CurrentUser.OpenSubKey(MainSubKey, true);
RegistryKey optionsKey;
optionsKey = mainKey.OpenSubKey(RegistryAppKey, true);
if (optionsKey == null)
{
mainKey.CreateSubKey(RegistryAppKey);
optionsKey = mainKey.OpenSubKey(RegistryAppKey, true);
}
optionsKey.SetValue(DbFolderPathString, dbFolderPath, RegistryValueKind.String);
optionsKey.SetValue(TempFolderPathString, tempFolderPath, RegistryValueKind.String);
optionsKey.Close();
mainKey.Close();
}
catch (Exception ex)
{
throw new RegistryAccessException("Could not commit configuration data to the registry.", ex);
}
}
/// <summary>
/// Checks whether the application settings exist inside the Windows Registry.
/// </summary>
/// <returns>True, if the application data exists inside the Windows Registry,
/// false, otherwise</returns>
public static bool RegistryConfigDataExists()
{
bool dataExists = false;
try
{
RegistryKey mainKey = Registry.CurrentUser.OpenSubKey(MainSubKey, true);
RegistryKey optionsKey;
optionsKey = mainKey.OpenSubKey(RegistryAppKey, true);
if (optionsKey == null)
{
mainKey.Close();
return dataExists;
}
string dbFolderPath = (string)optionsKey.GetValue(DbFolderPathString, String.Empty);
string tempFolderPath = (string)optionsKey.GetValue(TempFolderPathString, String.Empty);
optionsKey.Close();
mainKey.Close();
if ((dbFolderPath != String.Empty) && (tempFolderPath != String.Empty))
dataExists = true;
return dataExists;
}
catch (Exception)
{
return dataExists;
}
}
/// <summary>
/// Extracts the application settings from the Windows Registry.
/// </summary>
/// <param name="dbFolderPath">The folder containing the database file</param>
/// <param name="tempFolderPath">The application temporary folder</param>
public static void ExtractRegistrySettings(out string dbFolderPath, out string tempFolderPath)
{
try
{
RegistryKey mainKey = Registry.CurrentUser.OpenSubKey(MainSubKey, true);
RegistryKey optionsKey;
optionsKey = mainKey.OpenSubKey(RegistryAppKey);
if (optionsKey == null)
{
mainKey.Close();
throw new RegistryAccessException("Could not commit configuration data to the registry.",
null);
}
dbFolderPath = (string)optionsKey.GetValue(DbFolderPathString, String.Empty);
tempFolderPath = (string)optionsKey.GetValue(TempFolderPathString, String.Empty);
optionsKey.Close();
mainKey.Close();
if ((dbFolderPath == String.Empty) || (tempFolderPath == String.Empty))
throw new RegistryAccessException("Could not commit configuration data to the registry.",
null);
}
catch (Exception ex)
{
throw new RegistryAccessException("Could not commit configuration data to the registry.", ex);
}
}
/// <summary>
/// Deletes the application settings from the Windows Registry.
/// </summary>
public static void DeleteRegistrySettings()
{
try
{
RegistryKey mainKey = Registry.CurrentUser.OpenSubKey(MainSubKey, true);
mainKey.DeleteSubKey(RegistryAppKey);
mainKey.Close();
}
catch (Exception)
{
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.