|
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Text.RegularExpressions;
namespace RegexFindAndReplace
{
public enum PatternType
{
Regex,
Replacement
}
/// <summary>
/// The SettingsCache class contains methods for caching user settings in the registry.
/// </summary>
class SettingsCache
{
const int MAX_CACHE_COUNT = 15;
/// <summary>
/// Gets the registry key where all settings are stored.
/// </summary>
/// <returns></returns>
private RegistryKey GetRegistryKey()
{
string keyName = "SOFTWARE\\JeffHillman\\RegexFindAndReplace";
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey( keyName, true );
if ( registryKey == null )
{
registryKey = Registry.CurrentUser.CreateSubKey( keyName );
}
return registryKey;
}
/// <summary>
/// Gets the registry key for recently used regexes or replacements.
/// </summary>
/// <param name="regexType">Type of the regex.</param>
/// <returns></returns>
private RegistryKey GetPatternRegistryKey( PatternType patternType )
{
RegistryKey rootRegistryKey = GetRegistryKey();
RegistryKey registryKey = rootRegistryKey.OpenSubKey( patternType.ToString(), true );
if ( registryKey == null )
{
registryKey = rootRegistryKey.CreateSubKey( patternType.ToString() );
}
return registryKey;
}
/// <summary>
/// Gets the cached recently used regexes or replacements.
/// </summary>
/// <param name="patternType">Type of the pattern.</param>
/// <returns></returns>
public List<string> GetCachedPatterns( PatternType patternType )
{
List<string> cacheList = new List<string>();
RegistryKey registryKey = GetPatternRegistryKey( patternType );
try
{
for ( int i = 0; i < MAX_CACHE_COUNT; i++ )
{
string valueName = string.Format( "{0}{1}", patternType.ToString(), i );
object cachedPattern = registryKey.GetValue( valueName, null );
if ( cachedPattern != null )
{
cacheList.Add( cachedPattern.ToString() );
}
}
}
finally
{
registryKey.Close();
}
return cacheList;
}
/// <summary>
/// Adds a pattern to the list of recently used regexes or replacements
/// </summary>
/// <param name="option">The option.</param>
/// <param name="patternType">Type of the pattern.</param>
public void AddPattern( string pattern, PatternType patternType )
{
if ( !string.IsNullOrEmpty( pattern ) )
{
RegistryKey registryKey = GetPatternRegistryKey( patternType );
try
{
List<string> cachedPatternList = GetCachedPatterns( patternType );
if ( !cachedPatternList.Contains( pattern ) )
{
if ( cachedPatternList.Count == MAX_CACHE_COUNT )
{
cachedPatternList.RemoveAt( 0 );
}
cachedPatternList.Add( pattern );
}
for ( int i = 0; i < cachedPatternList.Count; i++ )
{
registryKey.SetValue( string.Format( "{0}{1}", patternType.ToString(), i ), cachedPatternList[ i ], RegistryValueKind.String );
}
}
finally
{
registryKey.Close();
}
}
}
/// <summary>
/// Moves a pattern to the top, to indicate that it was most recently used.
/// </summary>
/// <param name="pattern">The pattern.</param>
/// <param name="patternType">Type of the pattern.</param>
public void MovePatternToTop( string pattern, PatternType patternType )
{
if ( !string.IsNullOrEmpty( pattern ) )
{
RegistryKey registryKey = GetPatternRegistryKey( patternType );
try
{
List<string> cachedPatternList = GetCachedPatterns( patternType );
if ( cachedPatternList.Contains( pattern ) )
{
cachedPatternList.Remove( pattern );
cachedPatternList.Add( pattern );
}
for ( int i = 0; i < cachedPatternList.Count; i++ )
{
registryKey.SetValue( string.Format( "{0}{1}", patternType.ToString(), i ), cachedPatternList[ i ], RegistryValueKind.String );
}
}
finally
{
registryKey.Close();
}
}
}
/// <summary>
/// Gets the cached pattern at the specifed index.
/// </summary>
/// <param name="patternType">Type of the pattern.</param>
/// <param name="index">The index.</param>
/// <returns></returns>
public string GetCachedPatternAt( PatternType patternType, int index )
{
return GetCachedPatterns( patternType )[ GetCachedPatterns( patternType ).Count - index - 1 ];
}
/// <summary>
/// Determines whether the specified pattern has been cached.
/// </summary>
/// <param name="patternType">Type of the pattern.</param>
/// <param name="pattern">The pattern.</param>
/// <returns>
/// <c>true</c> if the specified pattern has been cached; otherwise, <c>false</c>.
/// </returns>
public bool IsPatternCached( PatternType patternType, string pattern )
{
return GetCachedPatterns( patternType ).Contains( pattern );
}
/// <summary>
/// Caches a generic string setting.
/// </summary>
/// <param name="name">The name of the setting.</param>
/// <param name="setting">The setting.</param>
public void CacheSetting( string name, string setting )
{
RegistryKey registryKey = GetRegistryKey();
registryKey.SetValue( name, setting, RegistryValueKind.String );
}
/// <summary>
/// Gets a generic cached setting.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="defaultSetting">The default setting.</param>
/// <returns>the setting if it exists in the registry; otherwise the specified default setting</returns>
public string GetCachedSetting( string name, string defaultSetting )
{
RegistryKey registryKey = GetRegistryKey();
return (string)registryKey.GetValue( name, (object)defaultSetting );
}
}
}
|
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.
I am a software developer currently working in Salt Lake City, Utah. I work primarily with C# for my job, but I mess around with C, Perl, and Windows PowerShell for fun.