How to: View Inherited and Local Configuration Settings Programmatically

Because each ASP.NET application inherits default configuration settings from the root Web.config file, you need to create Web.config files only for settings that override the default settings. If there are other Web.config files in the hierarchy, you might not know what default settings are inherited by your application, and therefore you might not know what to override.

This example uses the non-static method of obtaining configuration data, which allows you to pull configuration information from any application. If you are going to obtain configuration information from the application in which your code resides, use the static method, which processes faster. For more information, see the Working with Local and Remote Configuration Settings section in ASP.NET Configuration API Overview.

The following code example gets all of the configuration settings for an ASP.NET application called MyApp in the Default Web Site, and then writes the settings to an XML file.

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace SamplesAspNet.Config
    class GetFullConfig
        public static void Main(string[] args)
            Configuration config = WebConfigurationManager.OpenWebConfiguration("/MyApp");
            config.SaveAs("c:\\MyApp.web.config", ConfigurationSaveMode.Full, true);

Community Additions