共用方式為


Configuration.Save 方法

定義

將此 Configuration 物件內的設定寫入目前的 XML 設定檔。

多載

名稱 Description
Save()

將此 Configuration 物件內的設定寫入目前的 XML 設定檔。

Save(ConfigurationSaveMode)

將此 Configuration 物件內的設定寫入目前的 XML 設定檔。

Save(ConfigurationSaveMode, Boolean)

將此 Configuration 物件內的設定寫入目前的 XML 設定檔。

Save()

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
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 使用者將成為該檔案的新擁有者,並繼承賦予「創作者擁有者」的權限。 這會導致現有使用者的權限提升,而前任擁有者的權限則被移除。

適用於

Save(ConfigurationSaveMode)

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
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方法會根據參數在物件saveMode中保留設定Configuration

若設定檔不存在於該屬性所 FilePath 代表的物理位置,則會建立一個新的設定檔,以包含與繼承組態不同的設定。

若設定檔自建立此 Configuration 物件以來有所變動,則會發生執行時錯誤。

備註

當「創作者擁有者」在包含該設定檔的目錄的 ACL(存取控制清單)中列出時,目前的 Save 使用者將成為該檔案的新擁有者,並繼承賦予「創作者擁有者」的權限。 這會導致現有使用者的權限提升,而前任擁有者的權限則被移除。

適用於

Save(ConfigurationSaveMode, Boolean)

來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
Configuration.cs
來源:
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方法會根據 和 forceSaveAll 參數在物件saveMode中持續設定Configuration設定。

若設定檔不存在於該屬性所 FilePath 代表的物理位置,則會建立一個新的設定檔,以包含與繼承組態不同的設定。

若設定檔自建立此 Configuration 物件以來有所變動,則會發生執行時錯誤。

備註

當「創作者擁有者」在包含該設定檔的目錄的 ACL(存取控制清單)中列出時,目前的 Save 使用者將成為該檔案的新擁有者,並繼承賦予「創作者擁有者」的權限。 這會導致現有使用者的權限提升,而前任擁有者的權限則被移除。

適用於