/// <summary>
        /// Forces saving of the config file
        /// </summary>
        public void Save()
        {
            if (Configuration == null)
            {
                Console.Error.WriteLine("Error: ConfigFile " + FileName + " tried to save null object");
                return;
            }
            // Store watched property
            bool w = watched;

            // Disable watching of the file to avoid unnecessary triggering
            if (w == true)
            {
                Watched = false;
            }

            // Create folder to store in if needed
            ApplicationPreferences.CreateFolder(Folder);

            // Load from configuration
            XmlSerializer serializer = new XmlSerializer(SerializableObjectType);
            TextWriter    writer     = new StreamWriter(Path);

            serializer.Serialize(writer, Configuration);
            writer.Close();
            serializer = null;
            writer     = null;

            // Enable watching of the file if it was enabled before saving
            if (w == true)
            {
                Watched = true;
            }
        }
        /// <summary>
        /// Forces loading of the config file
        /// </summary>
        public void Load()
        {
            try {
                if (System.IO.File.Exists(Path) == true)
                {
                    try {
                        object newconfiguration = null;

                        XmlSerializer serializer = new XmlSerializer(SerializableObjectType);

                        TextReader reader = new StreamReader(Path);
                        newconfiguration = serializer.Deserialize(reader);
                        reader.Close();
                        reader = null;

                        // Sets Adaptor on new target
                        if (System.Data.Bindings.TypeValidator.IsCompatible(newconfiguration.GetType(), SerializableObjectType) == true)
                        {
                            ConfigAdaptor.Target = newconfiguration;
                            configuration        = null;
                            configuration        = newconfiguration;
                        }
                        else
                        {
                            Debug.Warning("ConfigFile.Load()",
                                          "Warning: configuration " + FileName + " was loaded but serializable type is doubtfull\n" +
                                          "         loadedType: " + newconfiguration.GetType() +
                                          " originalType: " + SerializableObjectType);
                        }
                    }
                    catch {
                        Debug.Warning("configuration " + FileName + " is corrupted, using defaults", "");
                        if (configuration == null)
                        {
                            RestoreToDefaults();
                        }
                    }
                }
                else
                if (configuration == null)
                {
                    RestoreToDefaults();
                }
            }
            finally {
                ApplicationPreferences.NotifyClients(Configuration);
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates ConfigFolder
        /// </summary>
        /// <param name="aName">
        /// Name of config folder <see cref="System.String"/>
        /// </param>
        public ConfigFolder(string aName)
        {
            if (aName == "")
            {
                if (ApplicationPreferences.GetMasterFolder() != null)
                {
                    throw new Exception("Error: There can't be two ConfigFolder master containers");
                }
            }
            else
            if (ApplicationPreferences.GetFolder(aName) != null)
            {
                throw new Exception("Error: There can't be two ConfigFolder with same name");
            }

            if (aName == "")
            {
                name = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
                return;
            }
            name = aName;
        }