{"id":11397,"date":"2016-04-06T10:40:13","date_gmt":"2016-04-06T15:40:13","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=11397"},"modified":"2016-04-22T11:36:42","modified_gmt":"2016-04-22T16:36:42","slug":"c-example-reading-configuration-values-from-app-config-file","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=11397","title":{"rendered":"C# Example: Reading Configuration Values from App.config File"},"content":{"rendered":"<div class='toc wptoc'>\n<h2>Contents<\/h2>\n<ol class='toc-odd level-1'>\n\t<li>\n\t\t<a href=\"#Prepare_Configuration_File\">Prepare Configuration File<\/a>\n\t<\/li>\n\t<li>\n\t\t<a href=\"#Read_Configuration_Values\">Read Configuration Values<\/a>\n\t<\/li>\n<\/ol>\n<\/ol>\n<\/div>\n<div class='wptoc-end'>&nbsp;<\/div>\n<span id=\"Prepare_Configuration_File\"><h2>Prepare Configuration File<\/h2><\/span>\n<p>* Create <em>App.config<\/em> file if it does not exist.<br \/>\n&#8211; For web applications, use <em>Web.config<\/em> file instead.<br \/>\n* Add a new section, e.g. <strong>myConfig<\/strong>, to <em>App.config<\/em> file:<\/p>\n<pre lang=\"xml\">\r\n<?xml version=\"1.0\"?>\r\n<configuration>\r\n  <configSections>\r\n    <section name=\"myConfig\" \r\n        type=\"System.Configuration.NameValueSectionHandler\" \/>\r\n  <\/configSections>\r\n\r\n  <myConfig>\r\n    <add key=\"key1\" value=\"value1\" \/>\r\n    <add key=\"key2\" value=\"value2\" \/>\r\n  <\/myConfig>\r\n<\/configuration>\r\n<\/pre>\n<span id=\"Read_Configuration_Values\"><h2>Read Configuration Values<\/h2><\/span>\n<p>* Add <em>System.Configuration<\/em> to References.<\/p>\n<pre lang=\"csharp\">\r\nusing log4net;\r\nusing System;\r\nusing System.Collections.Generic;\r\nusing System.Collections.Specialized;\r\nusing System.Configuration;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\n\r\nnamespace MyProj\r\n{\r\n    public class Properties\r\n    {\r\n        private static readonly ILog log = LogManager.GetLogger(typeof(Properties));\r\n\r\n        private static string key1;\r\n        public static string Key1\r\n        {\r\n            get\r\n            {\r\n                if (string.IsNullOrEmpty(key1))\r\n                {\r\n                    key1 = MyConfigVal(\"key1\");\r\n                }\r\n                return key1;\r\n            }\r\n        }\r\n\r\n        private static string key2;\r\n        public static string Key2\r\n        {\r\n            get\r\n            {\r\n                if (string.IsNullOrEmpty(key2))\r\n                {\r\n                    key2 = MyConfigVal(\"key2\");\r\n                }\r\n                return key2;\r\n            }\r\n        }\r\n\r\n        private static string MyConfigVal(string configName)\r\n        {\r\n            string val = MyConfig[configName];\r\n            if (val == null)\r\n            {\r\n                string err = string.Format(\"{0} not found in App.config.\", configName);\r\n                log.Fatal(err);\r\n                throw new ArgumentException(err);\r\n            }\r\n            return val;\r\n        }\r\n\r\n        private static NameValueCollection myConfig;\r\n        public static NameValueCollection MyConfig\r\n        {\r\n            get\r\n            {\r\n                if (myConfig != null)\r\n                {\r\n                    return myConfig;\r\n                }\r\n\r\n                myConfig = ConfigurationManager.GetSection(\r\n                    \"myConfig\") as NameValueCollection;\r\n                if (myConfig == null)\r\n                {\r\n                    throw new ArgumentException(\r\n                        (\"myConfig section not found in App.config.\"));\r\n                }\r\n                return myConfig;\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Prepare Configuration File * Create App.config file if it does not exist. &#8211; For web applications, use Web.config file instead. * Add a new section, e.g. myConfig, to App.config file: Read Configuration Values * Add System.Configuration to References. using log4net; &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=11397\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[556,335],"class_list":["post-11397","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-properties"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-2XP","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11397"}],"version-history":[{"count":5,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11397\/revisions"}],"predecessor-version":[{"id":11451,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/11397\/revisions\/11451"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}