Accessing Configuration Files Programmatically

Because the Web configuration files are XML documents, you could use the XML-handling objects (discussed in Chapter 10, “Using XML”) to store and retrieve information in Web.Config and Machine.Config. This might be useful when you want to store application-specific information in Web.Config, such as a database connection string.

However, you don't need to go to the trouble of parsing Web.Config to store and retrieve custom values from the file—there is an easier way. To store application-specific settings in Web.Config, simply create a section called appSettings and add the settings you want as key/value pairs in that section.

In the following example, the connection string is given the key “pubsdata”; ...

Get C#® Developer's Guide to ASP.NET, XML, and ADO.NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.