What would Brian Boitano do? C# Question on mundane tasks



  • There's always hundreds of different ways to do the one thing however when it comes to mundane tasks that always appear in just about every major application. Every way has their pros and cons, some will constantly eat that small bit more ram, others will eat clock cycles, sure the difference would not be noticeable to an end user but sti what way would you go about it?

     Let's take for example generating a recent files list, here in the an example a static class continuosly stores the last 10 files used, and saves to the registry every time it is modified.

    Would you take a similar approach, or would you take another approach, e.g. gets the list from the registry as needed saving that little bit of precious ram, but eating a few more cpu cycles?

    namespace BrianBoitano.Application
    {
        public static class RecentFilesList
        {
            private const string m_KEY = "RecentFiles";
            private const int m_maxFiles = 10;
            private static StringCollection m_list;

            public static void Initialize()
            {
                m_list = new StringCollection();
                string list = RegistryEditor.Get(m_KEY);
                if (list != null)
                {
                    string[] files = list.Split(new char[] { '|' });

                    foreach (string file in files)
                        m_list.Add(file);
                }
            }

            private static int FindFile(string file)
            {
                for (int i = 0; i < m_list.Count; i++)
                {
                    if (file.ToLower() == m_list[i].ToLower())
                        return i;
                }
                return -1;
            }


            private static void Save()
            {
                RegistryEditor.Set(m_KEY, ToString());
            }

            public static void Add(string file)
            {
                if (m_list == null) Initialize();
               
         int fileIndex = FindFile(file);

                if (fileIndex < 0)
                {
                    m_list.Insert(0, file);
                    while (m_list.Count > m_maxFiles)
                        m_list.RemoveAt(m_list.Count - 1);
                }
                else
                {
                    m_list.RemoveAt(fileIndex);
                    m_list.Insert(0, file);
                }

                Save();       
            }

            public static new string ToString()
            {
                StringBuilder files = new StringBuilder();
                bool first = true;

                if (m_list == null)
                    Initialize();

                foreach (string file in m_list)
                {
                    if (first)
                        first = false;
                    else
                        files.Append('|');
                    files.Append(file);
                }

                return files.ToString();
            }

            public static void Clear()
            {
                if (m_list == null) Initialize();
                else m_list.Clear();
                Save();
            }

        }
    }



  • Wouldn't it be easier to create one registry entry for each recent file?



  • I would probably read/write to the registry on the fly if only to address what I find as a major annoyance on some apps.  For example, I have two instances of Visual Studio open, and I make a config change to the environment.  I now need to make sure I close them in the right order so that the change sticks.  Same goes with the MRU lists, I believe.  Doing it on the fly would probably avoid the issue. 

     



  • @ammoQ said:

    Wouldn't it be easier to create one registry entry for each recent file?

    Hehe that would be the plan, I just quickly wrote this up as an example, and tested it with a regedit class that i had already written for a previous app.

    @lpope187 said:

    I would probably read/write to the registry on the fly if only to address what I find as a major annoyance on some apps.  For example, I have two instances of Visual Studio open, and I make a config change to the environment.  I now need to make sure I close them in the right order so that the change sticks.  Same goes with the MRU lists, I believe.  Doing it on the fly would probably avoid the issue. 

    Hehe,my example writes to the reg on the fly alright, but doesn't read on the fly, which would cause some issues of overwriting changes in another instance of the same app. I've actually experienced the same problem myself , but completely forgot about it when writing the above example, definately a very good point.



  • After going back in time to beat up Kubla Khan, Brian Boitano would fix your program by using Isolated Storage rather than the registry.

    The basic concept is sound, but Isolated Storage is the .NET way to do it.


Log in to reply