none
save a global variable in WinForm RRS feed

  • General discussion

  • Hi,

    I have some registry entries for my application. I am reading the values during the startup.

    How can I make the vaues available across the application and all its form without having to read it from the registry everytime?

    Monday, December 1, 2014 12:32 PM

All replies

  • You need to create somekind of an static data store for those registy values. One I have used in some simple applications is static class that wraps dictionary to get registry values.

    public static class RegistrySettings
    {
        private static Dictionary<string, object> settings;
    
        public static object ValueFor(string key)
        {
            if (settings == null)
            {
                //// read all needed registry key/values at first access
    
                settings = new Dictionary<string, object>();
    
                ReadFromRegistry();
            }
    
            return settings[key];
        }
    
        private static void ReadFromRegistry()
        {
            //// read registry
        }
    }

    Then you can use it like:

    private void btnGetGameSaveLocation_Click(object sender, EventArgs e)
    {
        string gamesFolder = (string)RegistrySettings.ValueFor("GamesFolder");
    
        /// ....
    }



    Monday, December 1, 2014 1:40 PM