Read contents of app config

Padmanabhan, Venkatesh 120 Reputation points
2023-04-06T16:30:57.14+00:00

Hi. I have a web.config file which contains the below . How to read the Key and value of each of the sections - TestSIS and MatchMMDS using c# ?

<configSections>
		<section name="TestSIS" type="System.Configuration.NameValueSectionHandler"/>
		<section name="MatchMMDS" type="System.Configuration.NameValueSectionHandler"/>
	</configSections>

<TestSIS>
		<add key="Daily" value="Test1D,Test2D,Test3D"/>
		<add key="Weekly" value="Test06W,Test07W"/>		
	</TestSIS>
	<MatchMMDS>
		<add key="Daily" value="Match01D,Match02D"/>
		<add key="Weekly" value="MatchI06W,MatchI07W,MatchI08W"/>
		<add key="Monthly" value="Match06M,Match07W,Match08W,Match09W"/>
	</MatchMMDS>
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,366 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sudipta Chakraborty - MSFT 1,101 Reputation points Microsoft Employee
    2023-04-06T16:55:55.1833333+00:00

    @Padmanabhan, Venkatesh You will need to add System.Configuration assembly reference to access configuration settings, using ConfigurationManager. To add the reference, just right click References and click to add references. In C# application you can use the following code:

    var applicationSettings = ConfigurationManager.GetSection("TestSIS") as NameValueCollection; // for reading MatchMMDS replace TestSIS to MatchMMDS
    
        if (applicationSettings.Count == 0)
        {
            Console.WriteLine("Application Settings are not defined");
        }
        else
        {
            foreach (var key in applicationSettings.AllKeys)
            {
                Console.WriteLine(key + " = " + applicationSettings[key]);
            }
        }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful