コード例 #1
0
        public static void PostInstallationInitialization()
        {
            Settings defaultSettings = null;

            // Load the default settings.
            string defaultFilePath = Settings.GetDefaultSettingsFilePath();

            if (File.Exists(defaultFilePath))
            {
                try
                {
                    XmlHelpers.LoadFromFile <Settings>("Settings", defaultFilePath, out defaultSettings);
                }
                catch (Exception exception)
                {
                    Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
                }
            }
            else
            {
                Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath);
            }

            // Load user settings if exists.
            Settings userSettings = null;
            string   userFilePath = Settings.GetUserSettingsFilePath();

            if (File.Exists(userFilePath))
            {
                try
                {
                    XmlHelpers.LoadFromFile <Settings>("Settings", userFilePath, out userSettings);
                }
                catch (Exception)
                {
                    System.IO.File.Delete(userFilePath);
                }

                if (userSettings != null)
                {
                    if (userSettings.SerializationVersion != Version)
                    {
                        Settings.MigrateSettingsToCurrentVersion(userSettings);

                        Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version);
                        userSettings.SerializationVersion = Version;
                    }

                    // Remove default settings.
                    if (userSettings.ConversionPresets != null)
                    {
                        for (int index = userSettings.ConversionPresets.Count - 1; index >= 0; index--)
                        {
                            if (userSettings.ConversionPresets[index].IsDefaultSettings)
                            {
                                userSettings.ConversionPresets.RemoveAt(index);
                            }
                        }
                    }
                }
            }

            Settings settings = userSettings != null?userSettings.Merge(defaultSettings) : defaultSettings;

            settings.Save();
        }
コード例 #2
0
        public static Settings Load()
        {
            Settings settings     = null;
            string   userFilePath = Settings.GetUserSettingsFilePath();

            if (File.Exists(userFilePath))
            {
                Settings userSettings = null;
                try
                {
                    XmlHelpers.LoadFromFile <Settings>("Settings", userFilePath, out userSettings);
                    settings = userSettings;
                }
                catch (Exception)
                {
                    MessageBoxResult messageBoxResult =
                        MessageBox.Show(
                            "Can't load file converter user settings. Do you want to fall back to default settings ?",
                            "Error",
                            MessageBoxButton.YesNo,
                            MessageBoxImage.Exclamation);

                    if (messageBoxResult == MessageBoxResult.Yes)
                    {
                        System.IO.File.Delete(userFilePath);
                        return(Settings.Load());
                    }
                    else if (messageBoxResult == MessageBoxResult.No)
                    {
                        return(null);
                    }
                }

                if (userSettings != null && userSettings.SerializationVersion != Version)
                {
                    Settings.MigrateSettingsToCurrentVersion(userSettings);

                    Diagnostics.Debug.Log("File converter settings has been imported from version {0} to version {1}.", userSettings.SerializationVersion, Version);
                    userSettings.SerializationVersion = Version;
                    userSettings.Save();
                }
            }
            else
            {
                // Load the default settings.
                string defaultFilePath = Settings.GetDefaultSettingsFilePath();
                if (File.Exists(defaultFilePath))
                {
                    Settings defaultSettings = null;
                    try
                    {
                        XmlHelpers.LoadFromFile <Settings>("Settings", defaultFilePath, out defaultSettings);
                        settings = defaultSettings;
                    }
                    catch (Exception exception)
                    {
                        Diagnostics.Debug.LogError("Fail to load file converter default settings. {0}", exception.Message);
                    }
                }
                else
                {
                    Diagnostics.Debug.LogError("Default settings not found at path {0}. You should try to reinstall the application.", defaultFilePath);
                }
            }

            return(settings);
        }