1. Colin Bate
  2. DownMarker

Source

DownMarker / downmarker.core / RecentlyUsedRegistryStore.cs

using System.Collections.Generic;
using System.Linq;
using Microsoft.Win32;

namespace DownMarker.Core
{
   public class RecentlyUsedRegistryStore : IRecentlyUsed
   {
      private const string REGKEY = @"software\downmarker\recentlyused";
      private const int MAX = 10;

      public IEnumerable<string> Files
      {
         get
         {
            var key = GetRegistryKey();
            return key.GetValueNames().Select(x => key.GetValue(x).ToString());
         }
      }

      private RegistryKey GetRegistryKey()
      {
         var key = Registry.CurrentUser.OpenSubKey(REGKEY, true);
         if (key == null)
         {
            return Registry.CurrentUser.CreateSubKey(REGKEY);
         }
         return key;
      }

      public void Touch(string uri)
      {
         // add or bump uri to the top
         var uris = new List<string>(this.Files);
         if (uris.Contains(uri))
         {
            uris.Remove(uri);
         }
         uris.Insert(0, uri);
         
         // save to registry
         var key = GetRegistryKey();
         foreach (var namedValue in 
             uris.Take(MAX).Select(
                (value,index)=>new {Name=index.ToString(),Value=value}))
         {
            key.SetValue(namedValue.Name, namedValue.Value);
         }
      }
   }
}