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

定义

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。Writes the configuration settings contained within this Configuration object to the current XML configuration file.

重载

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

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。Writes the configuration settings contained within this Configuration object to the current XML configuration file.

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

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。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)

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。Writes the configuration settings contained within this Configuration object to the current XML configuration file.

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

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。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 ()

异常

无法对配置文件执行写入操作。The configuration file could not be written to.

-or-

配置文件已更改。The configuration file has changed.

注解

Save方法将保留自创建此Configuration对象以来已修改的任何配置设置。The Save method persists any configuration settings that have been modified since this Configuration object was created. 如果配置文件不存在于FilePath属性表示的物理位置,则将创建一个新的配置文件,以包含与继承的配置不同的任何设置。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.

如果在创建此Configuration对象后配置文件发生了更改,则会发生运行时错误。If the configuration file has changed since this Configuration object was created, a run-time error occurs.

备注

如果 "创建者所有者" 列在包含配置文件的目录的 ACL (访问控制列表)中,则的当前用户Save将成为该文件的新所有者,并继承授予 "创建者所有者" 的权限。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)

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。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)

参数

saveMode
ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode

一个 ConfigurationSaveMode 值,该值用于确定要保存的属性值。A ConfigurationSaveMode value that determines which property values to save.

异常

无法对配置文件执行写入操作。The configuration file could not be written to.

-or-

配置文件已更改。The configuration file has changed.

示例

下面的代码示例演示如何使用Save方法保存自定义节。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

注解

Save方法Configuration根据参数saveMode保留对象中的配置设置。The Save method persists configuration settings in the Configuration object based on the saveMode parameter.

如果配置文件不存在于FilePath属性表示的物理位置,则将创建一个新的配置文件,以包含与继承的配置不同的任何设置。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.

如果在创建此Configuration对象后配置文件发生了更改,则会发生运行时错误。If the configuration file has changed since this Configuration object was created, a run-time error occurs.

备注

如果 "创建者所有者" 列在包含配置文件的目录的 ACL (访问控制列表)中,则的当前用户Save将成为该文件的新所有者,并继承授予 "创建者所有者" 的权限。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)

将包含在此 Configuration 对象中的配置设置写入当前 XML 配置文件。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)

参数

saveMode
ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode ConfigurationSaveMode

一个 ConfigurationSaveMode 值,该值用于确定要保存的属性值。A ConfigurationSaveMode value that determines which property values to save.

forceSaveAll
Boolean Boolean Boolean Boolean

如果在未修改配置的情况下保存配置,则为 true;否则,为 falsetrue to save even if the configuration was not modified; otherwise, false.

异常

无法对配置文件执行写入操作。The configuration file could not be written to.

- 或 --or-

配置文件已更改。The configuration file has changed.

注解

方法根据和参数forceSaveAll保留Configuration对象中的配置设置。 saveMode SaveThe Save method persists configuration settings in the Configuration object based on the saveMode and forceSaveAll parameters.

如果配置文件不存在于FilePath属性表示的物理位置,则将创建一个新的配置文件,以包含与继承的配置不同的任何设置。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.

如果在创建此Configuration对象后配置文件发生了更改,则会发生运行时错误。If the configuration file has changed since this Configuration object was created, a run-time error occurs.

备注

如果 "创建者所有者" 列在包含配置文件的目录的 ACL (访问控制列表)中,则的当前用户Save将成为该文件的新所有者,并继承授予 "创建者所有者" 的权限。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.

适用于