如何确认Settings.Default.Save()是否已写入用户配置文件



我想知道是否有可能知道Settings.Default.Save()方法是否已写入配置文件。由于代码中的一些问题,它可能无法持久。我怎么知道呢?

是否有任何回调,事件或返回值来知道?请帮帮我!!

Settings.Default Save() '被调用时调用的反汇编ClientSettingsStore:

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);
    }
}

深入了解调用堆栈(如userConfig.Save()),我们可以看到类MgmtConfigurationRecord类和方法SaveAs,它们也会抛出适当的异常。

如您所见,在错误的情况下会抛出异常。

:

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

就足够了。

最新更新