Пример #1
0
        /// <summary>Deletes the settings file.</summary>
        public virtual void Delete(string filename = null, SettingsOnFailure onFailure = SettingsOnFailure.Throw)
        {
            // Save and delete must not be interrupted or superseded by a SaveThreaded
            lock (_lock)
            {
                if (_saveThread != null) // this can only ever occur in the Sleep/lock wait phase of the quick save thread
                {
                    _saveThread.Abort();
                    _saveThread = null;
                }
                var attr = SettingsUtil.GetAttribute(GetType());
                if (filename == null)
                {
                    filename = attr.GetFileName();
                }

                if (onFailure == SettingsOnFailure.Throw)
                {
                    File.Delete(filename);
                }
                else if (onFailure == SettingsOnFailure.DoNothing)
                {
                    try { File.Delete(filename); }
                    catch { }
                }
                else
                {
                    while (true)
                    {
                        try
                        {
                            File.Delete(filename);
                            break;
                        }
                        catch (Exception e)
                        {
                            var choices = new List <string>()
                            {
                                "Try &again", "&Don't delete settings"
                            };
                            if (onFailure == SettingsOnFailure.ShowRetryWithCancel)
                            {
                                choices.Add("&Cancel");
                            }
                            int choice = DlgMessage.ShowWarning("Program settings could not be deleted.\n({0})\n\nWould you like to try again?".Fmt(e.Message), choices.ToArray());
                            if (choice == 1)
                            {
                                return;
                            }
                            if (choice == 2)
                            {
                                throw new SettingsCancelException();
                            }
                        }
                    }
                    ;
                }
            }
        }
Пример #2
0
 /// <summary>
 ///     <para>
 ///         Saves the settings.</para>
 ///     <para>
 ///         This method is fully compatible with <see cref="SettingsThreadedBase.SaveThreaded"/>, and will cancel any
 ///         pending earlier (older) saves.</para></summary>
 public virtual void Save(string filename = null, SettingsSerializer?serializer = null, SettingsOnFailure onFailure = SettingsOnFailure.Throw)
 {
     // Save and delete must not be interrupted or superseded by a SaveThreaded
     lock (_lock)
     {
         if (_saveThread != null) // this can only ever occur in the Sleep/lock wait phase of the quick save thread
         {
             _saveThread.Abort();
             _saveThread = null;
         }
         SettingsUtil.save(this, filename, serializer, onFailure);
     }
 }
Пример #3
0
        internal static void save(SettingsBase settings, string filename, SettingsSerializer?serializer, SettingsOnFailure onFailure)
        {
            var settingsType = settings.GetType();
            var attr         = GetAttribute(settingsType);

            if (filename == null)
            {
                filename = attr.GetFileName();
            }
            if (serializer == null)
            {
                serializer = attr.Serializer;
            }

            settings.BeforeSave();

            if (onFailure == SettingsOnFailure.Throw)
            {
                serialize(settings, settingsType, filename, serializer.Value);
            }
            else if (onFailure == SettingsOnFailure.DoNothing)
            {
                try { serialize(settings, settingsType, filename, serializer.Value); }
                catch { }
            }
            else
            {
                while (true)
                {
                    try
                    {
                        serialize(settings, settingsType, filename, serializer.Value);
                        break;
                    }
                    catch (Exception e)
                    {
                        var choices = new List <string>()
                        {
                            "Try &again",
                            "&Don't save settings",
                        };
                        int cancelIndex = -1;
                        if (onFailure == SettingsOnFailure.ShowRetryWithCancel)
                        {
                            cancelIndex = choices.Count;
                            choices.Add("&Cancel");
                        }
#if DEBUG
                        int breakIndex = choices.Count;
                        choices.Add("&Break debugger");
#endif
                        int choice = DlgMessage.ShowWarning("Program settings could not be saved.\n({0})\n\nWould you like to try again?".Fmt(e.Message), choices.ToArray());
                        if (choice == 1)
                        {
                            return;
                        }
                        if (choice == cancelIndex)
                        {
                            throw new SettingsCancelException();
                        }
#if DEBUG
                        if (choice == breakIndex)
                        {
                            System.Diagnostics.Debugger.Break();
                        }
#endif
                    }
                }
                ;
            }
        }