Configuration.Save Configuration.Save Configuration.Save Configuration.Save Method

Definition

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

Overloads

Save() Save() Save() Save()

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

Save(ConfigurationSaveMode) Save(ConfigurationSaveMode) Save(ConfigurationSaveMode) Save(ConfigurationSaveMode)

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

Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean)

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

Save() Save() Save() Save()

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

public:
 void Save();
public void Save ();
member this.Save : unit -> unit
Public Sub 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.

Note

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.

Save(ConfigurationSaveMode) Save(ConfigurationSaveMode) Save(ConfigurationSaveMode) Save(ConfigurationSaveMode)

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

public:
 void Save(System::Configuration::ConfigurationSaveMode mode);
public void Save (System.Configuration.ConfigurationSaveMode mode);
member this.Save : System.Configuration.ConfigurationSaveMode -> unit
Public Sub Save (mode As ConfigurationSaveMode)
Parameters
saveMode
ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode 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.


// 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());
    }

}
' Show how to create an instance of the Configuration class
' that represents this application configuration file.  
Public Shared Sub CreateConfigurationFile()
    Try

        ' Create a custom configuration section.
        Dim customSection As New CustomSection()

        ' Get the current configuration file.
        Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

        ' Create the section entry  
        ' in <configSections> and the 
        ' related target section in <configuration>.
        If config.Sections("CustomSection") Is Nothing Then
            config.Sections.Add("CustomSection", customSection)
        End If

        ' Create and add an entry to appSettings section.

        Dim conStringname As String = "LocalSqlServer"
        Dim conString As String = "data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
        Dim providerName As String = "System.Data.SqlClient"

        Dim connStrSettings As New ConnectionStringSettings()
        connStrSettings.Name = conStringname
        connStrSettings.ConnectionString = conString
        connStrSettings.ProviderName = providerName

        config.ConnectionStrings.ConnectionStrings.Add(connStrSettings)

        ' Add an entry to appSettings section.
        Dim appStgCnt As Integer = ConfigurationManager.AppSettings.Count
        Dim newKey As String = "NewKey" & appStgCnt.ToString()

        Dim newValue As String = Date.Now.ToLongDateString() & " " & Date.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 err As ConfigurationErrorsException
        Console.WriteLine("CreateConfigurationFile: {0}", err.ToString())
    End Try

End Sub

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.

Note

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.

Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean) Save(ConfigurationSaveMode, Boolean)

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

public:
 void Save(System::Configuration::ConfigurationSaveMode mode, bool forceUpdateAll);
public void Save (System.Configuration.ConfigurationSaveMode mode, bool forceUpdateAll);
member this.Save : System.Configuration.ConfigurationSaveMode * bool -> unit
Public Sub Save (mode As ConfigurationSaveMode, forceUpdateAll As Boolean)
Parameters
saveMode
ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode

A ConfigurationSaveMode value that determines which property values to save.

forceSaveAll
Boolean Boolean Boolean 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.

Note

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