Esempio n. 1
0
        /// <summary>
        /// Restore the user settings from the Windows registry.
        /// </summary>
        /// <param name="Type">The type of the settings object.</param>
        /// <param name="Section">The section in which the settings are stored.</param>
        /// <returns>A populated settings object.</returns>
        private static UserSettingsBase RestoreFromRegistry(Type Type, string Section)
        {
            string text;

            try
            {
                //text = Interaction.GetSetting(GetAppName(), Section, "Settings", "");
                text = "";
            }
            catch (Exception ex)
            {
                throw (new UserSettingsException("User settings not found in registry", ex));
            }

            try
            {
                MemoryStream buffer = new MemoryStream(Encoding.ASCII.GetBytes(text));

                XmlSerializer formatter = new XmlSerializer(Type);

                UserSettingsBase settings = ((UserSettingsBase)formatter.Deserialize(buffer));
                settings.mOption = UserStorageOption.Registry;

                return(settings);
            }
            catch (Exception ex)
            {
                throw (new UserSettingsException("User settings could not be loaded from registry", ex));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Restore the user settings from an XML file.
        /// </summary>
        /// <param name="Type">The type of the settings object.</param>
        /// <param name="Section">The section in which the settings are stored.</param>
        /// <returns>A populated settings object.</returns>
        private static UserSettingsBase RestoreFromFile(Type Type, string Section)
        {
            string path = GetFilePath(Section);

            FileStream inFile;

            try
            {
                inFile = File.OpenRead(path);
            }
            catch (Exception ex)
            {
                throw (new UserSettingsException("User settings file not found " + path, ex));
            }

            try
            {
                XmlSerializer formatter = new XmlSerializer(Type);

                byte[] buffer = new byte[Convert.ToInt32(inFile.Length) + 1];

                inFile.Read(buffer, 0, Convert.ToInt32(inFile.Length));
                MemoryStream stream = new MemoryStream(buffer);

                stream.Position = 0;

                UserSettingsBase settings = ((UserSettingsBase)formatter.Deserialize(stream));
                settings.mOption = UserStorageOption.File;
                return(settings);
            }
            catch (Exception ex)
            {
                throw (new UserSettingsException("User settings could not be read from file " + path, ex));
            }
            finally
            {
                inFile.Close();
            }
        }