Usman ur Rehman Ahmed's blog

Sample use of ResXResourceReader to store custom messages

Well that's pretty simple but might help someone out there. A newbie developer friend of mine was looking for a simple mechanism using which he can arrange different types of messages (errors, warnings, information) in resrouce file (helps achieve entries editing without application recompilation) for a simple windows utility. Quickly I created three resource files (much better designs are possible but that was relative to his needs) each having relative key, values pair to store appropriate mesages and created following class (ResourceReader.cs) to help him read the value against given key,

    public enum ResrouceType
    {
        Information
        Error,
        Warning
    }

    public class ResourceReader
    {
        #region | Methods [public] |

        public static string getValue(string key, ResrouceType resrouceType)
        {
            try
            {
                ResXResourceReader resourceReader = new ResXResourceReader(resrouceType.ToString() + ".resx");
                foreach (DictionaryEntry d in resourceReader)
                {
                    if (d.Key.ToString() == key)
                    {
                        return d.Value.ToString();
                    }
                }
                return string.Empty;
            }
            catch
            {
                throw;
            }
        }

        #endregion

    }

This is pretty simple and for those out there whom want to provide a rather basic handling of custom messages will serve the purpose using one line of call. An example to read value for key "HostNameRequired" from resrouce file "Information.resx",

    MessageBox.Show(ResourceReader.getValue("InfHostNameRequired",ResrouceType.Information));

To use above calss, create three resource files named "Error.resx", "Information.resx" and "Warning.resx" and place them at the executable application path (e.g. Debug/bin during development mode).

11
To Posterous, Love Metalab