Share via


Configuration.Save 方法

定義

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

多載

Save()

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

Save(ConfigurationSaveMode)

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

Save(ConfigurationSaveMode, Boolean)

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

Save()

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

public:
 void Save();
public void Save ();
member this.Save : unit -> unit
Public Sub Save ()

例外狀況

無法寫入組態檔。

-或-

組態檔已經變更。

備註

方法 Save 會保存自建立此 Configuration 對象以來已修改的任何組態設定。 如果組態檔不存在於 屬性所 FilePath 代表的實體位置,將會建立新的組態檔,以包含與繼承組態不同的任何設定。

如果組態檔自建立此 Configuration 對象之後已變更,就會發生運行時錯誤。

注意

當 [建立者擁有者] 列在包含組態檔之目錄的 ACL (存取控制 列表) 時,目前的使用者Save會成為檔案的新擁有者,並繼承授與給 'Creator Owner' 的許可權。 這會導致目前用戶的許可權提高,以及移除先前擁有者的許可權。

適用於

Save(ConfigurationSaveMode)

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

public:
 void Save(System::Configuration::ConfigurationSaveMode saveMode);
public void Save (System.Configuration.ConfigurationSaveMode saveMode);
member this.Save : System.Configuration.ConfigurationSaveMode -> unit
Public Sub Save (saveMode As ConfigurationSaveMode)

參數

saveMode
ConfigurationSaveMode

ConfigurationSaveMode 值,決定所要儲存的屬性值。

例外狀況

無法寫入組態檔。

-或-

組態檔已經變更。

範例

下列程式代碼範例示範如何使用 Save 方法來儲存自定義區段。


// 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根據 參數,將組態設定保存在物件中saveModeConfiguration

如果組態檔不存在於 屬性所 FilePath 代表的實體位置,將會建立新的組態檔,以包含與繼承組態不同的任何設定。

如果組態檔自建立此 Configuration 對象之後已變更,就會發生運行時錯誤。

注意

當 [建立者擁有者] 列在包含組態檔之目錄的 ACL (存取控制 列表) 時,目前的使用者Save會成為檔案的新擁有者,並繼承授與給 'Creator Owner' 的許可權。 這會導致目前用戶的許可權提高,以及移除先前擁有者的許可權。

適用於

Save(ConfigurationSaveMode, Boolean)

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs

將包含在這個 Configuration 物件中的組態設定寫入至目前的 XML 組態檔。

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

參數

saveMode
ConfigurationSaveMode

ConfigurationSaveMode 值,決定所要儲存的屬性值。

forceSaveAll
Boolean

true 表示即使尚未修改組態仍要儲存,否則為 false

例外狀況

無法寫入組態檔。

-或-

組態檔已經變更。

備註

方法會Save根據 saveModeforceSaveAll 參數,將組態設定保存在物件中Configuration

如果組態檔不存在於 屬性所 FilePath 代表的實體位置,將會建立新的組態檔,以包含與繼承組態不同的任何設定。

如果組態檔自建立此 Configuration 對象之後已變更,就會發生運行時錯誤。

注意

當 [建立者擁有者] 列在包含組態檔之目錄的 ACL (存取控制 列表) 時,目前的使用者Save會成為檔案的新擁有者,並繼承授與給 'Creator Owner' 的許可權。 這會導致目前用戶的許可權提高,以及移除先前擁有者的許可權。

適用於