예제 #1
0
        public ApplicationSettings Load(string filename)
        {
            ILegacySettingsResolver resolver = new DefaultLegacySettingsResolver();

            foreach (var factory in ReaderFactoryEnumeration(resolver, filename))
            {
                ISettingsReader reader = null;

                try
                {
                    // Create reader from factory.
                    reader = factory();

                    // Try to read settings.
                    ApplicationSettings settings = reader.Read();
                    if (settings != null)
                    {
                        Logger.Instance.WriteLine("Loaded settings from \"{0}\"", filename);

                        // Update current settings file.
                        if (CurrentSettingsFile != filename)
                        {
                            CurrentSettingsFile = filename;
                        }

                        return(settings);
                    }
                }
                // Stop iterating, file doesn't exist.
                catch (FileNotFoundException)
                {
                    break;
                }
                // Log other IO exceptions.
                catch (IOException e)
                {
                    Logger.Instance.WriteLine("Settings Error: [{0}] {1}", e.GetType().Name, e.Message);
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Dispose();
                    }
                }
            }

            // Failed to load.
            return(null);
        }
예제 #2
0
        public static ApplicationSettings Load(string path)
        {
            ILegacySettingsResolver resolver = new DefaultLegacySettingsResolver();

            foreach (Func <ISettingsReader> factory in ReaderFactoryEnumeration(resolver, path))
            {
                ISettingsReader settingsReader = null;

                try
                {
                    settingsReader = factory();

                    // Try to read settings.
                    var settings = settingsReader.Read();
                    if (settings != null)
                    {
                        Logger.Info($"Loaded settings from \"{path}\"");

                        return(settings);
                    }
                }
                catch (DirectoryNotFoundException)
                {
                    Logger.Info($"Failed to read \"{path}\": Folder does not exist.");
                    return(null);
                }
                catch (FileNotFoundException)
                {
                    // Stop iterating, file doesn't exist.
                    break;
                }
                catch (IOException e)
                {
                    // Log other IO exceptions.
                    Logger.Warn("Failed to read settings", e);
                }
                finally
                {
                    settingsReader?.Dispose();
                }
            }

            // Failed to load.
            return(null);
        }