|
using System;
using System.Collections.Specialized;
using Microsoft.Win32;
namespace SearcherAddIn
{
/// <summary>
/// RecentCache ��ժҪ˵����
/// </summary>
public enum CacheType
{
Find,
Replace
}
public class RecentCache
{
const int _MAX_CACHE_COUNT = 10;
const string _FIND_KEY = "SOFTWARE\\Niugang\\SearchAddIn\\FindCache";
const string _SEARCH_KEY = "SOFTWARE\\Niugang\\SearchAddIn\\SearchCache";
public RecentCache()
{
//
// TODO: �ڴ˴���ӹ��캯����
//
}
private RegistryKey getCacheKey(CacheType cacheType){
string keyName = cacheType == CacheType.Find?_FIND_KEY:_SEARCH_KEY;
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(keyName, true);
if(registryKey == null)
{
registryKey = Registry.CurrentUser.CreateSubKey(keyName);
}
return registryKey;
}
public StringCollection GetRecentList(CacheType cacheType){
StringCollection cacheList = new StringCollection();
RegistryKey registryKey = getCacheKey(cacheType);
try
{
cacheList = new StringCollection();
for(int i = 1; i <= _MAX_CACHE_COUNT; i++)
{
string cache = registryKey.GetValue(cacheType.ToString() + i) as string;
if(cache != null)
{
if(cache.Length > 0)
{
cacheList.Add(cache);
}
}
}
}
finally
{
registryKey.Close();
}
return cacheList;
}
public void Add(string content, CacheType cacheType)
{
RegistryKey registryKey = getCacheKey(cacheType);
try
{
StringCollection cacheList = GetRecentList(cacheType);
if(!cacheList.Contains(content))
{
if(cacheList.Count == _MAX_CACHE_COUNT)
cacheList.RemoveAt(0);
cacheList.Add(content);
}
for(int i = 1; i <= cacheList.Count; i++){
registryKey.SetValue(cacheType.ToString() + i, cacheList[i - 1]);
}
}
finally
{
registryKey.Close();
}
}
public void ClearRecentList(){
RegistryKey registryKey = getCacheKey(CacheType.Find);
try
{
foreach( string name in registryKey.GetValueNames() )
registryKey.DeleteValue( name );
}
finally
{
registryKey.Close();
}
registryKey = getCacheKey(CacheType.Replace);
try
{
foreach( string name in registryKey.GetValueNames() )
registryKey.DeleteValue( name );
}
finally
{
registryKey.Close();
}
}
public string GetCachePattern(CacheType cacheType, int index){
return GetRecentList(cacheType)[GetRecentList(cacheType).Count - 1 - index];
}
public bool IsCached(CacheType cacheType, string pattern){
return GetRecentList(cacheType).Contains(pattern);
}
}
}
|
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.