ApplicationSettingsBase.Save 方法

定义

存储应用程序设置属性的当前值。Stores the current values of the application settings properties.

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

示例

下面的代码示例演示从主窗体的 Closing 事件处理程序中调用的 Save 方法。The following code example shows the Save method being called from the Closing event handler for the primary form. 此方法还会在与窗体的 Text 属性关联的 settings 属性后面追加一个附加句点。This method also appends an extra period to the settings property that is associated with the form's Text property.

ApplicationSettingsBase 类概述中列出了完整的代码示例。The full code example is listed in the ApplicationSettingsBase class overview.

private:
    void AppSettingsForm_FormClosing(Object^ sender,
        FormClosingEventArgs^ e)
    {
        //Synchronize manual associations first.
        formSettings->FormText = this->Text + '.';
        formSettings->Save();
    }
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    //Synchronize manual associations first.
    frmSettings1.FormText = this.Text + '.';
    frmSettings1.FormSize = this.Size;
    frmSettings1.Save();
}
Private Sub Form1_FormClosing_1(ByVal sender As Object, ByVal e As _
        FormClosingEventArgs) Handles MyBase.FormClosing
    'Synchronize manual associations first.
    frmSettings1.FormText = Me.Text + "."c

    ' Save size settings manually.
    frmSettings1.FormSize = Me.Size

    frmSettings1.Save()
End Sub

注解

Save 方法将每个设置属性的当前值写入其关联的数据存储区。The Save method writes the current value of each settings property to its associated data store. 对于每个属性,此方法将调用关联的设置提供程序上的 SetPropertyValues 方法。For each property, this method calls the SetPropertyValues method on the associated settings provider.

此方法与基类实现的不同之处在于,它会在写入值之前引发 SettingsSaving 事件。This method differs from the base class implementation in that it raises the SettingsSaving event before the values are written.

如果唯一定义的设置是应用程序范围的设置,Save 将不起作用,并且在使用默认 LocalFileSettingsProvider调用时不会返回错误。If the only settings defined are application-scoped settings, Save will have no effect and return no error if called with the default LocalFileSettingsProvider. LocalFileSettingsProvider 仅保存用户范围的设置。LocalFileSettingsProvider only saves user-scoped settings.

重要

没有相应的 Load 方法,因为应用程序设置的值会在包装类初始化期间自动加载。There is no corresponding Load method because the values of application settings are automatically loaded during wrapper class initialization. 与此相反,当应用程序结束时,这些值不会自动保存。In contrast, these values are not automatically saved when an application ends. 因此,你必须显式调用 Save 方法,以保留应用程序设置的当前值。Therefore, you must explicitly call the Save method to persist the current values of the application settings. 这通常在主或包含 FormClosing 事件处理程序中执行。This is typically performed in the Closing event handler of the primary or containing Form.

适用于

另请参阅