void saveSettingsType(string type, SettingsCollection settings)
        {
            this.disableWatcher(type);
            try
            {
                var filePath = getFileFromType(type);

                if (settings == null || settings.Count == 0)
                {
                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    return;
                }

                // Cover the case where the file still has a lock on it from a previous IO access and needs time to let go.
                var retryAttempt = 0;
                while (true)
                {
                    try
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
                            using (var writer = new StreamWriter(stream))
                            {
                                settings.Persist(writer);
                                break;
                            }
                    }
                    catch (Exception)
                    {
                        if (retryAttempt < 5)
                        {
                            Thread.Sleep(1000);
                            retryAttempt++;
                        }
                        else
                        {
                            throw;
                        }
                    }
                }
            }
            finally
            {
                this.enableWatcher(type);
            }
        }
 public void SaveSettings(string type, SettingsCollection settings)
 {
     saveSettingsType(type, settings);
 }
Beispiel #3
0
 public void SaveSettings(string type, SettingsCollection settings)
 {
     _settingsDictionary[type] = settings;
 }