简体   繁体   中英

How to Confirm if Settings.Default.Save() has written into the user config file

I want to know if there is any possibility to know whether the Settings.Default.Save() method has written to the config file. Due to some issues in the code it could fail to persist. How will I come to know about that?

Is there any callbacks, or Events or return value to know that? Please help me!!

Dissambled ClientSettingsStore that is invoked when Settings.Default Save()` is called:

internal void WriteSettings(string sectionName, bool isRoaming, IDictionary newSettings)
{
    if (!ConfigurationManagerInternalFactory.Instance.SupportsUserConfig)
         throw new ConfigurationErrorsException(System.SR.GetString("UserSettingsNotSupported"));
    ...
    if (configSection == null)
         throw new ConfigurationErrorsException(System.SR.GetString("SettingsSaveFailedNoSection"));
    ...
    try
    {
        userConfig.Save();
    }
    catch (ConfigurationErrorsException ex)
    {
         throw new ConfigurationErrorsException(System.SR.GetString("SettingsSaveFailed", new object[1]
              {
               (object) ex.Message
              }), (Exception) ex);
    }
}

Going deeper in call stack (fe userConfig.Save() ) brings us to class MgmtConfigurationRecord class and method SaveAs that also throws approperiate exceptions.

As you can see, in case of errors exceptions are thrown.

So:

try
{ 
   Settings.Default.Save();
}
catch (Exception ex)
{
   //sth went wrong
}

is enough in my opinion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM