Click here to Skip to main content
15,891,907 members
Articles / Programming Languages / C#

Standard Regular Expression Searcher Addin For VS.NET 2003

Rate me:
Please Sign up or sign in to vote.
4.88/5 (42 votes)
11 Jan 2005CPOL3 min read 107.1K   930   52  
Standard Regular Expression Searcher add-in for VS.NET 2003.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions