AppSettingsSection.File Property

Definition

Gets or sets a configuration file that provides additional settings or overrides the settings specified in the appSettings element.

[System.Configuration.ConfigurationProperty("file", DefaultValue="")]
public string File { get; set; }
public string File { get; set; }

Property Value

A configuration file that provides additional settings or overrides the settings specified in the appSettings element.

Attributes

Examples

The following example stores additional appSettings values in an external configuration file and saves the name of this file in the File property.

// This function shows how to use the File property of the
// appSettings section.
// The File property is used to specify an auxiliary 
// configuration file.
// Usually you create an auxiliary file off-line to store 
// additional settings that you can modify as needed without
// causing an application restart,as in the case of a Web 
// application.
// These settings are then added to the ones defined in the
// application configuration file.
static void  IntializeConfigurationFile()
{
    // Create a set of unique key/value pairs to store in
    // the appSettings section of an auxiliary configuration
    // file.
    string time1 = String.Concat(DateTime.Now.ToLongDateString(),
                           " ", DateTime.Now.ToLongTimeString());

    string time2 = String.Concat(DateTime.Now.ToLongDateString(),
                           " ", new DateTime(2009, 06, 30).ToLongTimeString());
   
    string[] buffer = {"<appSettings>",
    "<add key='AuxAppStg0' value='" + time1 + "'/>", 
    "<add key='AuxAppStg1' value='" + time2 + "'/>",
    "</appSettings>"};

    // Create an auxiliary configuration file and store the
    // appSettings defined before.
    // Note creating a file at run-time is just for demo 
    // purposes to run this example.
    File.WriteAllLines("auxiliaryFile.config", buffer);
    
    // Get the current configuration associated
    // with the application.
    System.Configuration.Configuration config =
       ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

    // Associate the auxiliary with the default
    // configuration file. 
    System.Configuration.AppSettingsSection appSettings = config.AppSettings;
    appSettings.File = "auxiliaryFile.config";
    
    // Save the configuration file.
    config.Save(ConfigurationSaveMode.Modified);

    // Force a reload in memory of the 
    // changed section.
    ConfigurationManager.RefreshSection("appSettings");
}

Applies to

Product Versions
.NET 8 (package-provided), 9 (package-provided)
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7 (package-provided), 4.7, 4.7.1 (package-provided), 4.7.1, 4.7.2 (package-provided), 4.7.2, 4.8 (package-provided), 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

See also