public void AddConfiguration(ProfileConfiguration configuration)
        {
            if (configuration == null) throw new ArgumentNullException();
            if (string.IsNullOrWhiteSpace(configuration.Name)) throw new ArgumentNullException("Configuration name must be specified");

            if (this[configuration.Name] != null)
                throw new ArgumentException("There is already a configuration named " + configuration.Name);

            _profileConfigurations.Add(configuration);
        }
        public static void SaveProfileConfiguration(string name, ProfileConfiguration config)
        {
            lock (LockObject)
            {
                var configuration = LoadConfigurationInternal(Settings.SettingsFilePath);
                configuration.RemoveConfiguration(name);

                configuration.AddConfiguration(config);

                SaveConfigurationInternal(configuration, Settings.SettingsFilePath);
            }
        }
        public ProfileConfigViewModel(ProfileConfiguration config)
        {
            _config = config;

            AppSettings.AddingNew += (sender, e) => { e.NewObject = new KeyValueViewModel(); };
            AppSettings.ListChanged += (sender, e) =>
            {
                if (e.ListChangedType == ListChangedType.ItemAdded)
                {

                }

            };
        }
        public void AddConfiguration(ProfileConfiguration configuration)
        {
            if (configuration == null)
            {
                throw new ArgumentNullException();
            }
            if (string.IsNullOrWhiteSpace(configuration.Name))
            {
                throw new ArgumentNullException("Configuration name must be specified");
            }

            if (this[configuration.Name] != null)
            {
                throw new ArgumentException("There is already a configuration named " + configuration.Name);
            }

            _profileConfigurations.Add(configuration);
        }
        private void UpdateSettings(IEnumerable<System.IO.FileInfo> filesToUpdate, ProfileConfiguration config)
        {
            foreach (var file in filesToUpdate)
            {
                var configFile = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(new System.Configuration.ConfigurationFileMap(file.FullName));
                configFile.AppSettings.Settings.Clear();
                configFile.ConnectionStrings.ConnectionStrings.Clear();

                foreach (var appSetting in config.AppSettings)
                    configFile.AppSettings.Settings.Add(appSetting.Name, appSetting.Value);
                foreach (var connectionString in config.ConnectionStrings)
                    configFile.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings(connectionString.Name, connectionString.Value, connectionString.Provider));

                configFile.Save();
            }
        }
        public static void SaveProfileConfiguration(string name, ProfileConfiguration config)
        {
            lock (LockObject)
            {
                var configuration = LoadConfigurationInternal(Settings.SettingsFilePath);
                configuration.RemoveConfiguration(name);

                configuration.AddConfiguration(config);

                SaveConfigurationInternal(configuration, Settings.SettingsFilePath);
            }
        }
        private static void UpdateSettings(IEnumerable <System.IO.FileInfo> filesToUpdate, ProfileConfiguration config)
        {
            foreach (var file in filesToUpdate)
            {
                var configFile = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(new System.Configuration.ConfigurationFileMap(file.FullName));
                configFile.AppSettings.Settings.Clear();
                configFile.ConnectionStrings.ConnectionStrings.Clear();

                foreach (var appSetting in config.AppSettings)
                {
                    configFile.AppSettings.Settings.Add(appSetting.Name, appSetting.Value);
                }
                foreach (var connectionString in config.ConnectionStrings)
                {
                    var connectionConfig = new System.Configuration.ConnectionStringSettings(connectionString.Name, connectionString.Value);
                    if (!string.IsNullOrWhiteSpace(connectionString.Provider))
                    {
                        connectionConfig.ProviderName = connectionString.Provider;
                    }

                    configFile.ConnectionStrings.ConnectionStrings.Add(connectionConfig);
                }

                configFile.Save();
            }
        }
        public void ImportSettings()
        {
            var config = Configuration.Load();

            if (config.Contains(ConfigurationName))
            {
                MessageBox.Show("Failed to import settings. A configuration named '" + ConfigurationName + "' already exists. Please rename it and try again.", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            var profileConfig = new ProfileConfiguration();
            profileConfig.Name = ConfigurationName;

            foreach (var connectionString in ConnectionStringList)
                profileConfig.AddConnectionString(connectionString.Key, connectionString.Value, connectionString.Provider);
            foreach (var setting in AppSettingsList)
                profileConfig.AddAppSetting(setting.Key, setting.Value);

            config.AddConfiguration(profileConfig);

            config.Save();

            _events.GetEvent<ImportSettingsCompleteEvent>().Publish(ConfigurationName);
        }