Configuration.Save Method

Definition

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

Overloads

Save()

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

Save(ConfigurationSaveMode)

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

Save(ConfigurationSaveMode, Boolean)

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

Save()

Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

C#
public void Save();

Exceptions

The configuration file could not be written to.

-or-

The configuration file has changed.

Remarks

The Save method persists any configuration settings that have been modified since this Configuration object was created. If a configuration file does not exist at the physical location represented by the FilePath property, a new configuration file will be created to contain any settings that are different from the inherited configuration.

If the configuration file has changed since this Configuration object was created, a run-time error occurs.

Napomena

When 'Creator Owner' is listed in the ACL (Access Control List) of the directory containing the configuration file, the current user of Save becomes the new owner of the file and inherits the permissions granted to 'Creator Owner'. This results in an elevation of privileges for the current user and a removal of privileges for the previous owner.

Applies to

.NET 10 (package-provided) i druge verzije
Proizvod Verzije
.NET 8 (package-provided), 9 (package-provided), 10 (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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(ConfigurationSaveMode)

Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

C#
public void Save(System.Configuration.ConfigurationSaveMode saveMode);

Parameters

saveMode
ConfigurationSaveMode

A ConfigurationSaveMode value that determines which property values to save.

Exceptions

The configuration file could not be written to.

-or-

The configuration file has changed.

Examples

The following code example demonstrates how to use the Save method to save a custom section.

C#

// Show how to create an instance of the Configuration class
// that represents this application configuration file.  
static void CreateConfigurationFile()
{
    try
    {

        // Create a custom configuration section.
        CustomSection customSection = new CustomSection();

        // Get the current configuration file.
        System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                ConfigurationUserLevel.None);

        // Create the custom section entry  
        // in <configSections> group and the 
        // related target section in <configuration>.
        if (config.Sections["CustomSection"] == null)
        {
            config.Sections.Add("CustomSection", customSection);
        }

        // Create and add an entry to appSettings section.
        
        string conStringname="LocalSqlServer";
        string conString = @"data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true";
        string providerName="System.Data.SqlClient";

        ConnectionStringSettings connStrSettings = new ConnectionStringSettings();
        connStrSettings.Name = conStringname;
        connStrSettings.ConnectionString= conString;
        connStrSettings.ProviderName = providerName;

        config.ConnectionStrings.ConnectionStrings.Add(connStrSettings);
        
        // Add an entry to appSettings section.
        int appStgCnt =
            ConfigurationManager.AppSettings.Count;
        string newKey = "NewKey" + appStgCnt.ToString();

        string newValue = DateTime.Now.ToLongDateString() +
          " " + DateTime.Now.ToLongTimeString();

        config.AppSettings.Settings.Add(newKey, newValue);

        // Save the configuration file.
        customSection.SectionInformation.ForceSave = true;
        config.Save(ConfigurationSaveMode.Full);

        Console.WriteLine("Created configuration file: {0}",
            config.FilePath);
    }
    catch (ConfigurationErrorsException err)
    {
        Console.WriteLine("CreateConfigurationFile: {0}", err.ToString());
    }
}

Remarks

The Save method persists configuration settings in the Configuration object based on the saveMode parameter.

If a configuration file does not exist at the physical location represented by the FilePath property, a new configuration file will be created to contain any settings that are different from the inherited configuration.

If the configuration file has changed since this Configuration object was created, a run-time error occurs.

Napomena

When 'Creator Owner' is listed in the ACL (Access Control List) of the directory containing the configuration file, the current user of Save becomes the new owner of the file and inherits the permissions granted to 'Creator Owner'. This results in an elevation of privileges for the current user and a removal of privileges for the previous owner.

Applies to

.NET 10 (package-provided) i druge verzije
Proizvod Verzije
.NET 8 (package-provided), 9 (package-provided), 10 (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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Save(ConfigurationSaveMode, Boolean)

Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs
Source:
Configuration.cs

Writes the configuration settings contained within this Configuration object to the current XML configuration file.

C#
public void Save(System.Configuration.ConfigurationSaveMode saveMode, bool forceSaveAll);

Parameters

saveMode
ConfigurationSaveMode

A ConfigurationSaveMode value that determines which property values to save.

forceSaveAll
Boolean

true to save even if the configuration was not modified; otherwise, false.

Exceptions

The configuration file could not be written to.

-or-

The configuration file has changed.

Remarks

The Save method persists configuration settings in the Configuration object based on the saveMode and forceSaveAll parameters.

If a configuration file does not exist at the physical location represented by the FilePath property, a new configuration file will be created to contain any settings that are different from the inherited configuration.

If the configuration file has changed since this Configuration object was created, a run-time error occurs.

Napomena

When 'Creator Owner' is listed in the ACL (Access Control List) of the directory containing the configuration file, the current user of Save becomes the new owner of the file and inherits the permissions granted to 'Creator Owner'. This results in an elevation of privileges for the current user and a removal of privileges for the previous owner.

Applies to

.NET 10 (package-provided) i druge verzije
Proizvod Verzije
.NET 8 (package-provided), 9 (package-provided), 10 (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, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10