Total Pageviews

Friday, 10 July 2015

Accessing resource file content in C#

To Get the value of a specific key from resource file:

        public string gtresource(string keyName)
        {
            string value = null;
            System.Resources.ResourceManager RM = new System.Resources.ResourceManager("MyApp.Resources.Resource", this.GetType().Assembly);
            value = RM.GetString(keyName).ToString();
            if (value != null && value != "")
            {
                return value;
            }
            else
            {
                return "";
            }
        }


To get all the key value pairs from resource file:

        public Dictionary<string, string> gtresource()
        {
            System.Resources.ResourceManager RM = new System.Resources.ResourceManager("MyApp.Resources.Resource", this.GetType().Assembly);
            Dictionary<string, string> dic = new Dictionary<string, string>();
            System.Resources.ResourceSet resourceSet = RM.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
            foreach (System.Collections.DictionaryEntry item in resourceSet)
            {
                dic.Add(item.Key.ToString(), item.Value.ToString());
            }
            return dic;
        }