コード例 #1
0
ファイル: AppSettings.cs プロジェクト: gokudo-graves/LibLSLCC
        public static void Load()
        {
            bool firstRun = !Directory.Exists(AppDataDir);

            try
            {
                Directory.CreateDirectory(AppDataDir);
            }
            // ReSharper disable once CatchAllClause
            catch (Exception e)
            {
                MessageBox.Show(
                    "The application settings directory could not be created." + Environment.NewLine +
                    "The application will now exit."
                    + Environment.NewLine
                    + Environment.NewLine + "Error: " + e.Message,
                    "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Environment.Exit(1);
            }


            try
            {
                if (!File.Exists(SettingsVersionFile))
                {
                    File.WriteAllText(SettingsVersionFile, CurrentSettingsVersion);
                }
                else
                {
                    var versionCheck = File.ReadAllText(SettingsVersionFile);
                    if (versionCheck != CurrentSettingsVersion)
                    {
                        var r = MessageBox.Show(
                            "A version change in the application settings has been detected, default settings will be applied over your old settings."
                            + Environment.NewLine +
                            "Click cancel if you do not want this to happen and would just like to exit for now instead.", "Settings Version Change",
                            MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

                        if (r == DialogResult.Cancel)
                        {
                            Environment.Exit(1);
                        }

                        if (File.Exists(SettingsFile))
                        {
                            File.Delete(SettingsFile);
                        }

                        File.WriteAllText(SettingsVersionFile, CurrentSettingsVersion);
                    }
                }
            }
            // ReSharper disable once CatchAllClause
            catch (Exception e)
            {
                MessageBox.Show(
                    "There was a problem reading/writing the application settings files, " +
                    "please make sure they are not locked by another application.  "
                    + "The application will now exit."
                    + Environment.NewLine
                    + Environment.NewLine + "Error: " + e.Message, "Settings Read/Write Error",
                    MessageBoxButtons.OK);

                Environment.Exit(1);
            }


            try
            {
                SettingsManager.Load(SettingsFile);
            }

            catch (FileNotFoundException)
            {
                if (!firstRun)
                {
                    MessageBox.Show(
                        "There was a problem with the application settings, the application settings file could not be found."
                        + Environment.NewLine + Environment.NewLine +
                        "The file will be created and default settings will be applied.",
                        "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                SettingsManager.ApplyDefaults();
                Save();
            }
            catch (IOException)
            {
                MessageBox.Show(
                    "There was a problem reading the application settings file, it may be locked by another application." +
                    Environment.NewLine +
                    "The application will now exit.",
                    "Configuration Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                Environment.Exit(1);
            }
            // ReSharper disable once CatchAllClause
            catch (Exception)
            {
                var r = MessageBox.Show(
                    "There was a problem/syntax error with the application settings, default settings will be re-applied."
                    + Environment.NewLine + Environment.NewLine +
                    "Click cancel if you do not want this to happen and would just like to exit for now instead.",
                    "Configuration Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);


                if (r == DialogResult.Cancel)
                {
                    Environment.Exit(1);
                }

                SettingsManager.ApplyDefaults();
                Save();
            }
        }