Beispiel #1
0
        public void Load(string profile)
        {
            foreach (var section in this.Sections)
            {
                if (section.IsInitialized)
                {
                    continue;
                }
                section.InitializeComponent();
            }
            var fileName = Profiles.GetFileName(profile);

            if (!File.Exists(fileName))
            {
                Logger.Write(this, LogLevel.Debug, "Configuration file \"{0}\" does not exist.", fileName);
                return;
            }
            Logger.Write(this, LogLevel.Debug, "Loading configuration from file \"{0}\".", fileName);
            try
            {
                var modifiedElements = this.GetModifiedElements();
                var restoredElements = new List <ConfigurationElement>();
                using (var stream = File.OpenRead(fileName))
                {
                    var sections = Serializer.Load(stream);
                    foreach (var section in sections)
                    {
                        if (!this.Contains(section.Key))
                        {
                            //If config was created by a component that is no longer loaded then it will be lost here.
                            //TODO: Add the config but hide it so it's preserved but not displayed.
                            Logger.Write(this, LogLevel.Warn, "Configuration section \"{0}\" no longer exists.", section.Key);
                            continue;
                        }
                        var existing = this.GetSection(section.Key);
                        try
                        {
                            Logger.Write(this, LogLevel.Debug, "Loading configuration section \"{0}\".", section.Key);
                            restoredElements.AddRange(this.Load(existing, section.Value));
                        }
                        catch (Exception e)
                        {
                            Logger.Write(this, LogLevel.Warn, "Failed to load configuration section \"{0}\": {1}", existing.Id, e.Message);
                        }
                    }
                }
                foreach (var modifiedElement in modifiedElements)
                {
                    if (restoredElements.Contains(modifiedElement))
                    {
                        continue;
                    }
                    Logger.Write(this, LogLevel.Debug, "Resetting configuration element: \"{0}\".", modifiedElement.Id);
                    modifiedElement.Reset();
                }
                Profiles.Profile = profile;
            }
            catch (Exception e)
            {
                Logger.Write(this, LogLevel.Warn, "Failed to load configuration: {0}", e.Message);
            }
        }