Example #1
0
        /// <summary>
        /// Save application settings for XML file
        /// </summary>
        public void SaveSettings()
        {
            PasswordMangerSettings settings = new PasswordMangerSettings()
            {
                PrimaryPath = PrimarySavePath, SecondaryPaths = SecondarySavePaths
            };
            XmlSerializer serializer = new XmlSerializer(typeof(PasswordMangerSettings));

            if (File.Exists(SettingsFilePath))
            {
                File.Copy(SettingsFilePath, SettingsFilePath + ".backup", true);
            }

            try
            {
                StreamWriter writer = new StreamWriter(SettingsFilePath);
                serializer.Serialize(writer, settings);
                writer.Close();
            }
            catch
            {
                if (File.Exists(SettingsFilePath))
                {
                    File.Delete(SettingsFilePath);                                  // rollback if something failed
                }
                if (File.Exists(SettingsFilePath + ".backup"))
                {
                    File.Move(SettingsFilePath + ".backup", SettingsFilePath);
                }
                throw new Exception("Unable to save File.");
            }
            finally
            {
                if (File.Exists(SettingsFilePath + ".backup"))
                {
                    File.Delete(SettingsFilePath + ".backup");                                              // remove backup file
                }
            }
        }
Example #2
0
        /// <summary>
        /// Load settings from XML
        /// </summary>
        public void LoadSettings()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(PasswordMangerSettings));

            try
            {
                StreamReader           reader   = new StreamReader(SettingsFilePath);
                PasswordMangerSettings settings = (PasswordMangerSettings)serializer.Deserialize(reader);

                PrimarySavePath = settings.PrimaryPath;
                SecondarySavePaths.Clear();
                foreach (string path in settings.SecondaryPaths)
                {
                    SecondarySavePaths.Add(path);
                }

                reader.Close();
            }
            catch
            {
                throw new Exception("Unable to load settings. Configuration file corrupted.");
            }
        }