private static bool DecryptEncryptConnectionString(string path)
        {
            ExeConfigurationFileMap file = new ExeConfigurationFileMap();
            file.ExeConfigFilename = path;
            ConfigurationUserLevel level = new ConfigurationUserLevel();

            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, level);
            ConfigurationSection section = config.GetSection("connectionStrings");

            // Encrypt Configuration ConnectionString
            if (!section.SectionInformation.IsProtected)
            {
                section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
                section.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);
                return true;
            }

            // Decrypt Configuration ConnectionString
            else
            {
                section.SectionInformation.UnprotectSection();
                config.Save();
                return false;
            }
        }
 private static string OpenRemoteConnectionString(string path, string name)
 {
     ExeConfigurationFileMap file = new ExeConfigurationFileMap();
     file.ExeConfigFilename = path;
     ConfigurationUserLevel level = new ConfigurationUserLevel();
     Configuration config = ConfigurationManager.OpenMappedExeConfiguration(file, level);
     return config.ConnectionStrings.ConnectionStrings[name].ConnectionString;
 }
Example #3
0
        protected void InitialiseConfigurationWith(ConfigurationUserLevel userLevel, string configurationFileContents)
        {
            File.WriteAllText(ConfigurationFileTemporaryFilePath, configurationFileContents);

            var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = ConfigurationFileTemporaryFilePath };

            Configuration = ConfigurationManager.OpenMappedExeConfiguration(fileMap, userLevel);
        }
 private static System.Configuration.Configuration OpenExeConfigurationImpl(ConfigurationFileMap fileMap, bool isMachine, ConfigurationUserLevel userLevel, string exePath)
 {
     if ((!isMachine && (((fileMap == null) && (exePath == null)) || ((fileMap != null) && (((ExeConfigurationFileMap) fileMap).ExeConfigFilename == null)))) && ((s_configSystem != null) && (s_configSystem.GetType() != typeof(ClientConfigurationSystem))))
     {
         throw new ArgumentException(System.Configuration.SR.GetString("Config_configmanager_open_noexe"));
     }
     return ClientConfigurationHost.OpenExeConfiguration(fileMap, isMachine, userLevel, exePath);
 }
 private System.Configuration.Configuration GetClientConfig(ConfigurationUserLevel userLevel)
 {
     if (UseDefaultConfig(SettingsFileMap, userLevel))
     {
         return ConfigurationManager.OpenExeConfiguration(userLevel);
     }
     return ConfigurationManager.OpenMappedExeConfiguration(SettingsFileMap, userLevel);
 }
Example #6
0
 public static string GetDefaultExeConfigPath(ConfigurationUserLevel userLevel) {
     try {
         var UserConfig = ConfigurationManager.OpenExeConfiguration(userLevel);
         return UserConfig.FilePath;
     }
     catch (ConfigurationException e) {
         return e.Filename;
     }
 }
 public static string GetDefaultExeConfigPath(ConfigurationUserLevel userLevel)
 {
     try
     {
         return ConfigurationManager.OpenExeConfiguration(userLevel).FilePath;
     }
     catch (ConfigurationException exception)
     {
         return exception.Filename;
     }
 }
Example #8
0
		public static void LoadConfiguration(ExeConfigurationFileMap FnMap = null, ConfigurationUserLevel CuLevel = ConfigurationUserLevel.None)
		{
            if (FnMap == null)
                appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationPath);
            else
                appConfig = ConfigurationManager.OpenMappedExeConfiguration(FnMap, CuLevel);
			ConfigurationSection section = appConfig.Sections["installer"];
			if (section == null)
				throw new ConfigurationErrorsException("installer section not found in " + appConfig.FilePath);
			string strXml = section.SectionInformation.GetRawXml();
			xmlConfig = new XmlDocument();
			xmlConfig.LoadXml(strXml);
		}
Example #9
0
 public static void SetAndSave(string name, string value, ConfigurationUserLevel level = ConfigurationUserLevel.None)
 {
     var config = ConfigurationManager.OpenExeConfiguration(level);
     if (!config.AppSettings.Settings.AllKeys.Contains(name))
     {
         config.AppSettings.Settings.Add(name, value);
     }
     else
     {
         config.AppSettings.Settings[name].Value = value;
     }
     config.Save(ConfigurationSaveMode.Modified);
     ConfigurationManager.RefreshSection("appSettings");
 }
Example #10
0
        protected IConfiguration GetConfiguration(ConfigurationUserLevel userLevel)
        {
            // Configuration flows from Machine.config -> exe.config -> roaming user.config -> local user.config with
            // latter definitions trumping earlier (e.g. local trumps roaming, which trumps exe, etc.).
            //
            // Opening configuration other than None will provide a combined view of with roaming or roaming and local.
            // As we want to handle the consolodation ourselves we need to open twice. Once to get the actual user config
            // path, then again with the path explicitly specified with "None" for our user level. (Values are lazily
            // loaded so this isn't a terrible perf issue.)
            IConfiguration configuration = this.ConfigurationManager.OpenConfiguration(userLevel);

            if (userLevel == ConfigurationUserLevel.None)
            {
                return configuration;
            }
            else
            {
                return this.ConfigurationManager.OpenConfiguration(configuration.FilePath);
            }
        }
        /// <summary>
        /// Gets the current applications &lt;OlapConfigSection&gt; section.
        /// </summary>
        /// <param name="ConfigLevel">
        /// The &lt;ConfigurationUserLevel&gt; that the config file
        /// is retrieved from.
        /// </param>
        /// <returns>
        /// The configuration file's &lt;OlapConfigSection&gt; section.
        /// </returns>
        public static OlapConfigSectionSettings GetSection(ConfigurationUserLevel ConfigLevel)
        {
            /*
             * This class is setup using a factory pattern that forces you to
             * name the section &lt;OlapConfigSection&gt; in the config file.
             * If you would prefer to be able to specify the name of the section,
             * then remove this method and mark the constructor public.
             */
            System.Configuration.Configuration Config = ConfigurationManager.OpenExeConfiguration
                (ConfigLevel);
            OlapConfigSectionSettings oOlapConfigSectionSettings;

            oOlapConfigSectionSettings =
                (OlapConfigSectionSettings)Config.GetSection("OlapConfigSectionSettings");
            if (oOlapConfigSectionSettings == null) {
                oOlapConfigSectionSettings = new OlapConfigSectionSettings();
                Config.Sections.Add("OlapConfigSectionSettings", oOlapConfigSectionSettings);
            }
            oOlapConfigSectionSettings._Config = Config;

            return oOlapConfigSectionSettings;
        }
Example #12
0
		internal static Configuration OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, Assembly calling_assembly, string exePath)
		{
			ExeConfigurationFileMap map = new ExeConfigurationFileMap ();

			/* Roaming and RoamingAndLocal should be different

			On windows,
			  PerUserRoaming = \Documents and Settings\<username>\Application Data\...
			  PerUserRoamingAndLocal = \Documents and Settings\<username>\Local Settings\Application Data\...
			*/

			switch (userLevel) {
			case ConfigurationUserLevel.None:
				if (exePath == null || exePath.Length == 0) {
					map.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
				} else {
					if (!Path.IsPathRooted (exePath))
						exePath = Path.GetFullPath (exePath);
					if (!File.Exists (exePath)) {
						Exception cause = new ArgumentException ("The specified path does not exist.", "exePath");
						throw new ConfigurationErrorsException ("Error Initializing the configuration system:", cause);
					}
					map.ExeConfigFilename = exePath + ".config";
				}
				break;
			case ConfigurationUserLevel.PerUserRoaming:
				map.RoamingUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), GetAssemblyInfo(calling_assembly));
				map.RoamingUserConfigFilename = Path.Combine (map.RoamingUserConfigFilename, "user.config");
				goto case ConfigurationUserLevel.None;

			case ConfigurationUserLevel.PerUserRoamingAndLocal:
				map.LocalUserConfigFilename = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.LocalApplicationData), GetAssemblyInfo(calling_assembly));
				map.LocalUserConfigFilename = Path.Combine (map.LocalUserConfigFilename, "user.config");
				goto case ConfigurationUserLevel.PerUserRoaming;
			}

			return ConfigurationFactory.Create (typeof(ExeConfigurationHost), map, userLevel);
		}
 public IConfiguration OpenConfiguration(ConfigurationUserLevel userLevel)
 {
     Configuration configuration = ConfigurationManager.OpenExeConfiguration(userLevel);
     return configuration == null ? null : new ConfigurationWrapper(configuration);
 }
 /// <inheritdoc />
 public global::System.Configuration.Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap pExeConfigurationFileMap, ConfigurationUserLevel pConfigurationUserLevel)
 {
     return ConfigurationManager.OpenMappedExeConfiguration(pExeConfigurationFileMap, pConfigurationUserLevel);
 }
        private static bool UseDefaultConfig(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
        {
            if (fileMap != null)
            {
                switch (userLevel)
                {
                    case ConfigurationUserLevel.None:
                        return string.IsNullOrEmpty(fileMap.ExeConfigFilename);

                    case ConfigurationUserLevel.PerUserRoaming:
                        return string.IsNullOrEmpty(fileMap.RoamingUserConfigFilename);

                    case ConfigurationUserLevel.PerUserRoamingAndLocal:
                        return string.IsNullOrEmpty(fileMap.LocalUserConfigFilename);
                }
            }
            return true;
        }
Example #16
0
 /// <inheritdoc />
 public global::System.Configuration.Configuration OpenExeConfiguration(ConfigurationUserLevel pConfigurationUserLevel)
 {
     return(ConfigurationManager.OpenExeConfiguration(pConfigurationUserLevel));
 }
 private static void Initialize()
 {
     if (UserSettingsLevel == ConfigurationUserLevel.None)
     {
         SettingsConfigurationSection configSection = ConfigurationManager.GetSection("settingsConfiguration/settingsProvider") as SettingsConfigurationSection;
         if (configSection != null)
         {
             UserSettingsLevel = configSection.UserLevel;
             SettingsFileMap = ReadConfigurationFileMap(configSection);
         }
         UserSettingsLevel = GetValidUserLevel(UserSettingsLevel);
         if (UseDefaultConfig(SettingsFileMap, UserSettingsLevel))
         {
             FUserConfigPath = GetDefaultExeConfigPath(UserSettingsLevel);
         }
         else if (UserSettingsLevel == ConfigurationUserLevel.PerUserRoaming)
         {
             FUserConfigPath = SettingsFileMap.RoamingUserConfigFilename;
         }
         else
         {
             FUserConfigPath = SettingsFileMap.LocalUserConfigFilename;
         }
     }
 }
 public static void Reinitialize()
 {
     UserSettingsLevel = ConfigurationUserLevel.None;
     SettingsFileMap = null;
     SpecialFolderMap = null;
     ConfigurationManager.RefreshSection("settingsConfiguration/settingsProvider");
     ConfigurationManager.RefreshSection("settingsConfiguration/specialFolders");
     Initialize();
 }
        [Category("NotWorking")] // bug #323622
        public void OpenExeConfiguration1_Remote()
        {
            AppDomain domain = null;
            string    config_file;
            string    config_xml = @"
				<configuration>
					<appSettings>
						<add key='anyKey' value='42' />
					</appSettings>
				</configuration>"                ;

            config_file = Path.Combine(tempFolder, "otherConfig.noconfigext");
            File.WriteAllText(config_file, config_xml);

            try
            {
                AppDomainSetup setup = new AppDomainSetup();
                setup.ConfigurationFile = config_file;
                setup.ApplicationBase   = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                domain = AppDomain.CreateDomain("foo", null, setup);

                RemoteConfig config = RemoteConfig.GetInstance(domain);

                ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
                Assert.AreEqual(config_file, config.GetFilePath(userLevel));
                Assert.AreEqual("42", config.GetSettingValue(userLevel, "anyKey"));
                Assert.AreEqual("42", config.GetSettingValue("anyKey"));
            }
            finally
            {
                if (domain != null)
                {
                    AppDomain.Unload(domain);
                }
                File.Delete(config_file);
            }

            config_file = Path.Combine(tempFolder, "otherConfig.exe.config");
            File.WriteAllText(config_file, config_xml);

            try
            {
                AppDomainSetup setup = new AppDomainSetup();
                setup.ConfigurationFile = config_file;
                setup.ApplicationBase   = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                domain = AppDomain.CreateDomain("foo", null, setup);

                RemoteConfig config = RemoteConfig.GetInstance(domain);

                ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
                Assert.AreEqual(config_file, config.GetFilePath(userLevel));
                Assert.AreEqual("42", config.GetSettingValue(userLevel, "anyKey"));
                Assert.AreEqual("42", config.GetSettingValue("anyKey"));
            }
            finally
            {
                if (domain != null)
                {
                    AppDomain.Unload(domain);
                }
                File.Delete(config_file);
            }

            try
            {
                AppDomainSetup setup = new AppDomainSetup();
                setup.ConfigurationFile = config_file;
                setup.ApplicationBase   = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                domain = AppDomain.CreateDomain("foo", null, setup);

                RemoteConfig config = RemoteConfig.GetInstance(domain);

                ConfigurationUserLevel userLevel = ConfigurationUserLevel.None;
                Assert.AreEqual(config_file, config.GetFilePath(userLevel));
                Assert.IsNull(config.GetSettingValue(userLevel, "anyKey"));
                Assert.IsNull(config.GetSettingValue("anyKey"));
            }
            finally
            {
                if (domain != null)
                {
                    AppDomain.Unload(domain);
                }
                File.Delete(config_file);
            }
        }
 private static string GetPreviousUserConfigPath(ConfigurationUserLevel userLevel)
 {
     Version version;
     if (userLevel == ConfigurationUserLevel.None)
     {
         return null;
     }
     string defaultExeConfigPath = GetDefaultExeConfigPath(userLevel);
     string directoryName = Path.GetDirectoryName(defaultExeConfigPath);
     defaultExeConfigPath = Path.GetFileName(defaultExeConfigPath);
     string path = Path.GetDirectoryName(directoryName);
     if (!Directory.Exists(path))
     {
         return null;
     }
     if (!VersionHelper.TryParse(Path.GetFileName(directoryName), VersionStyles.AllowMajorMinorBuildRevision, out version))
     {
         return null;
     }
     List<Version> source = new List<Version>();
     foreach (string str4 in Directory.GetDirectories(path))
     {
         Version version2;
         if (VersionHelper.TryParse(Path.GetFileName(str4), VersionStyles.AllowMajorMinorBuildRevision, out version2) && (version2 != version))
         {
             source.Add(version2);
         }
     }
     if (source.Count == 0)
     {
         return null;
     }
     if (source.Count > 1)
     {
         source.Sort();
     }
     return Path.Combine(Path.Combine(path, source.Last<Version>().ToString()), defaultExeConfigPath);
 }
 public System.Configuration.Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel, bool preLoad)
 {
     return(ConfigurationManager.OpenMappedExeConfiguration(fileMap, userLevel, preLoad));
 }
 public string GetFilePath(ConfigurationUserLevel userLevel)
 {
     global::System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(userLevel);
     return(config.FilePath);
 }
 public System.Configuration.Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)
 {
     return(ConfigurationManager.OpenExeConfiguration(userLevel));
 }
 /// <inheritdoc />
 public System.Configuration.Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap pExeConfigurationFileMap, ConfigurationUserLevel pConfigurationUserLevel)
 {
     return(ConfigurationManager.OpenMappedExeConfiguration(pExeConfigurationFileMap, pConfigurationUserLevel));
 }
 public virtual System.Configuration.Configuration OpenMappedExeConfiguration(ExeConfigurationFileMap fileMap, ConfigurationUserLevel userLevel)
 {
     return(ConfigurationManager.OpenMappedExeConfiguration(fileMap, userLevel));
 }
		private void LoadProperties (ExeConfigurationFileMap exeMap, SettingsPropertyCollection collection, ConfigurationUserLevel level, string sectionGroupName, bool allowOverwrite, string groupName)
		{
			Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap,level);
			
			ConfigurationSectionGroup sectionGroup = config.GetSectionGroup (sectionGroupName);
			if (sectionGroup != null) {
				foreach (ConfigurationSection configSection in sectionGroup.Sections) {
					if (configSection.SectionInformation.Name != groupName)
						continue;

					ClientSettingsSection clientSection = configSection as ClientSettingsSection;
					if (clientSection == null)
						continue;

					foreach (SettingElement element in clientSection.Settings) {
						LoadPropertyValue(collection, element, allowOverwrite);
					}
					// Only the first one seems to be processed by MS
					break;
				}
			}

		}
 public static Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)
 {
     return(OpenExeConfigurationInternal(userLevel, Assembly.GetEntryAssembly(), null));
 }
Example #28
0
 public static System.Configuration.Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel = ConfigurationUserLevel.None) => ConfigurationManager.OpenExeConfiguration(userLevel);
Example #29
0
        private void SaveProperties(ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
        {
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, level);

            UserSettingsGroup userGroup = config.GetSectionGroup("userSettings") as UserSettingsGroup;
            bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

            if (userGroup == null)
            {
                userGroup = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", userGroup);
            }
            ApplicationSettingsBase asb       = context.CurrentSettings;
            string class_name                 = NormalizeInvalidXmlChars((asb != null ? asb.GetType() : typeof(ApplicationSettingsBase)).FullName);
            ClientSettingsSection userSection = null;
            ConfigurationSection  cnf         = userGroup.Sections.Get(class_name);

            userSection = cnf as ClientSettingsSection;
            if (userSection == null)
            {
                userSection = new ClientSettingsSection();
                userGroup.Sections.Add(class_name, userSection);
            }

            bool hasChanges = false;

            if (userSection == null)
            {
                return;
            }

            foreach (SettingsPropertyValue value in collection)
            {
                if (checkUserLevel && value.Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                {
                    continue;
                }
                // The default impl does not save the ApplicationScopedSetting properties
                if (value.Property.Attributes.Contains(typeof(ApplicationScopedSettingAttribute)))
                {
                    continue;
                }

                hasChanges = true;
                SettingElement element = userSection.Settings.Get(value.Name);
                if (element == null)
                {
                    element = new SettingElement(value.Name, value.Property.SerializeAs);
                    userSection.Settings.Add(element);
                }
                if (element.Value.ValueXml == null)
                {
                    element.Value.ValueXml = new XmlDocument().CreateElement("value");
                }
                switch (value.Property.SerializeAs)
                {
                case SettingsSerializeAs.Xml:
                    element.Value.ValueXml.InnerXml = StripXmlHeader(value.SerializedValue as string);
                    break;

                case SettingsSerializeAs.String:
                    element.Value.ValueXml.InnerText = value.SerializedValue as string;
                    break;

                case SettingsSerializeAs.Binary:
                    element.Value.ValueXml.InnerText = value.SerializedValue != null?Convert.ToBase64String(value.SerializedValue as byte []) : string.Empty;

                    break;

                default:
                    throw new NotImplementedException();
                }
            }
            if (hasChanges)
            {
                config.Save(ConfigurationSaveMode.Minimal, true);
            }
        }
 private static ConfigurationUserLevel GetValidUserLevel(ConfigurationUserLevel userLevel)
 {
     switch (userLevel)
     {
         case ConfigurationUserLevel.PerUserRoaming:
         case ConfigurationUserLevel.PerUserRoamingAndLocal:
             return userLevel;
     }
     return ConfigurationUserLevel.PerUserRoamingAndLocal;
 }
Example #31
0
        public override void InitForConfiguration(ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
        {
            map = (ExeConfigurationFileMap)hostInitConfigurationParams [0];

            if (hostInitConfigurationParams.Length > 1 &&
                hostInitConfigurationParams [1] is ConfigurationUserLevel)
            {
                level = (ConfigurationUserLevel)hostInitConfigurationParams [1];
            }
            if (locationSubPath == null)
            {
                switch (level)
                {
                case ConfigurationUserLevel.PerUserRoaming:
                    if (map.RoamingUserConfigFilename == null)
                    {
                        throw new ArgumentException("RoamingUserConfigFilename must be set correctly");
                    }
                    locationSubPath = "roaming";
                    break;

                case ConfigurationUserLevel.PerUserRoamingAndLocal:
                    if (map.LocalUserConfigFilename == null)
                    {
                        throw new ArgumentException("LocalUserConfigFilename must be set correctly");
                    }
                    locationSubPath = "local";
                    break;
                }
            }

            configPath = null;
            string next = null;

            locationConfigPath = null;

            if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null)
            {
                configPath         = "exe";
                next               = "local";
                locationConfigPath = map.ExeConfigFilename;
            }

            if (locationSubPath == "local" && map.LocalUserConfigFilename != null)
            {
                configPath         = "local";
                next               = "roaming";
                locationConfigPath = map.LocalUserConfigFilename;
            }

            if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null)
            {
                configPath         = "roaming";
                next               = "machine";
                locationConfigPath = map.RoamingUserConfigFilename;
            }

            if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null)
            {
                configPath = "machine";
                next       = null;
            }
            locationSubPath = next;
        }
 private Dictionary<string, SettingElement> ReadSettings(ConfigurationUserLevel userLevel, string sectionName)
 {
     System.Configuration.Configuration clientConfig = this.GetClientConfig(SkipUserConfig ? ConfigurationUserLevel.None : userLevel);
     string str = (userLevel == ConfigurationUserLevel.None) ? "applicationSettings/" : "userSettings/";
     return this.ReadSettings(clientConfig, str + sectionName);
 }
		static void CheckFileMap (ConfigurationUserLevel level, ExeConfigurationFileMap map)
		{
			switch (level) {
			case ConfigurationUserLevel.None:
				if (string.IsNullOrEmpty (map.ExeConfigFilename))
					throw new ArgumentException (
						"The 'ExeConfigFilename' argument cannot be null.");
				break;
			case ConfigurationUserLevel.PerUserRoamingAndLocal:
				if (string.IsNullOrEmpty (map.LocalUserConfigFilename))
					throw new ArgumentException (
						"The 'LocalUserConfigFilename' argument cannot be null.");
				goto case ConfigurationUserLevel.PerUserRoaming;
			case ConfigurationUserLevel.PerUserRoaming:
				if (string.IsNullOrEmpty (map.RoamingUserConfigFilename))
					throw new ArgumentException (
						"The 'RoamingUserConfigFilename' argument cannot be null.");
				goto case ConfigurationUserLevel.None;
			}
		}
 private void RevertToParent(ConfigurationUserLevel userLevel, string sectionName)
 {
     System.Configuration.Configuration clientConfig = this.GetClientConfig(userLevel);
     bool flag = false;
     if (UseDefaultConfig(SettingsFileMap, userLevel))
     {
         ClientSettingsSection section = this.GetUserSection(clientConfig, "userSettings/" + sectionName, false);
         if (section != null)
         {
             section.SectionInformation.RevertToParent();
             flag = true;
         }
     }
     else
     {
         ConfigurationSectionGroup sectionGroup = clientConfig.GetSectionGroup("userSettings");
         if (sectionGroup != null)
         {
             sectionGroup.SectionGroups.Remove(sectionName);
             flag = true;
         }
     }
     if (flag)
     {
         clientConfig.Save();
     }
 }
Example #35
0
 /// <inheritdoc />
 public IConfiguration OpenExeConfiguration(ConfigurationUserLevel pConfigurationUserLevel)
 {
     return(new ConfigurationWrap(ConfigurationManager.OpenExeConfiguration(pConfigurationUserLevel)));
 }
 private void WriteSettings(string sectionName, ConfigurationUserLevel userLevel, List<SettingsPropertyValue> newSettings)
 {
     System.Configuration.Configuration clientConfig = this.GetClientConfig(userLevel);
     ClientSettingsSection section = this.GetUserSection(clientConfig, sectionName, true);
     if (section == null)
     {
         throw new ConfigurationErrorsException("Failed to save settings. No settings section found");
     }
     foreach (SettingsPropertyValue value2 in newSettings)
     {
         SettingElement element = section.Settings.Get(value2.Name);
         if (element == null)
         {
             element = new SettingElement {
                 Name = value2.Name
             };
             section.Settings.Add(element);
         }
         element.SerializeAs = value2.Property.SerializeAs;
         element.Value.ValueXml = this.SerializeToXmlElement(value2.Property, value2);
     }
     try
     {
         clientConfig.Save();
     }
     catch (ConfigurationErrorsException exception)
     {
         throw new ConfigurationErrorsException(string.Format("Failed to save settings. {0}", exception.Message), exception);
     }
 }
        internal static Configuration OpenExeConfiguration(ConfigurationFileMap fileMap, bool isMachine,
                                                           ConfigurationUserLevel userLevel, string exePath)
        {
            // validate userLevel argument
            switch (userLevel)
            {
            case ConfigurationUserLevel.None:
            case ConfigurationUserLevel.PerUserRoaming:
            case ConfigurationUserLevel.PerUserRoamingAndLocal:
                break;

            default:
                throw ExceptionUtil.ParameterInvalid(nameof(userLevel));
            }

            // validate fileMap arguments
            if (fileMap != null)
            {
                if (string.IsNullOrEmpty(fileMap.MachineConfigFilename))
                {
                    throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(fileMap.MachineConfigFilename));
                }

                ExeConfigurationFileMap exeFileMap = fileMap as ExeConfigurationFileMap;
                if (exeFileMap != null)
                {
                    switch (userLevel)
                    {
                    case ConfigurationUserLevel.None:
                        if (string.IsNullOrEmpty(exeFileMap.ExeConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.ExeConfigFilename));
                        }
                        break;

                    case ConfigurationUserLevel.PerUserRoaming:
                        if (string.IsNullOrEmpty(exeFileMap.RoamingUserConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.RoamingUserConfigFilename));
                        }
                        goto case ConfigurationUserLevel.None;

                    case ConfigurationUserLevel.PerUserRoamingAndLocal:
                        if (string.IsNullOrEmpty(exeFileMap.LocalUserConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.LocalUserConfigFilename));
                        }
                        goto case ConfigurationUserLevel.PerUserRoaming;
                    }
                }
            }

            string configPath = null;

            if (isMachine)
            {
                configPath = MachineConfigPath;
            }
            else
            {
                switch (userLevel)
                {
                case ConfigurationUserLevel.None:
                    configPath = ExeConfigPath;
                    break;

                case ConfigurationUserLevel.PerUserRoaming:
                    configPath = RoamingUserConfigPath;
                    break;

                case ConfigurationUserLevel.PerUserRoamingAndLocal:
                    configPath = LocalUserConfigPath;
                    break;
                }
            }

            Configuration configuration = new Configuration(null, typeof(ClientConfigurationHost), fileMap, exePath, configPath);

            return(configuration);
        }
Example #38
0
 /// <inheritdoc />
 public IConfiguration OpenMappedExeConfiguration(ExeConfigurationFileMap pExeConfigurationFileMap, ConfigurationUserLevel pConfigurationUserLevel)
 {
     return(new ConfigurationWrap(ConfigurationManager.OpenMappedExeConfiguration(pExeConfigurationFileMap, pConfigurationUserLevel)));
 }
        private void SaveProperties(ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
        {
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, level);

            UserSettingsGroup userGroup = config.GetSectionGroup("userSettings") as UserSettingsGroup;
            bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

#if true // my reimplementation
            if (userGroup == null)
            {
                userGroup = new UserSettingsGroup();
                config.SectionGroups.Add("userSettings", userGroup);
                ApplicationSettingsBase asb = context.CurrentSettings;
                ClientSettingsSection   cs  = new ClientSettingsSection();
                userGroup.Sections.Add((asb != null ? asb.GetType() : typeof(ApplicationSettingsBase)).FullName, cs);
            }

            bool hasChanges = false;

            foreach (ConfigurationSection section in userGroup.Sections)
            {
                ClientSettingsSection userSection = section as ClientSettingsSection;
                if (userSection == null)
                {
                    continue;
                }

                foreach (SettingsPropertyValue value in collection)
                {
                    if (checkUserLevel && value.Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                    {
                        continue;
                    }
                    hasChanges = true;
                    SettingElement element = userSection.Settings.Get(value.Name);
                    if (element == null)
                    {
                        element = new SettingElement(value.Name, value.Property.SerializeAs);
                        userSection.Settings.Add(element);
                    }
                    if (element.Value.ValueXml == null)
                    {
                        element.Value.ValueXml = new XmlDocument().CreateElement("value");
                    }
                    switch (value.Property.SerializeAs)
                    {
                    case SettingsSerializeAs.Xml:
                        element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
                        break;

                    case SettingsSerializeAs.String:
                        element.Value.ValueXml.InnerText = value.SerializedValue as string;
                        break;

                    case SettingsSerializeAs.Binary:
                        element.Value.ValueXml.InnerText = value.SerializedValue != null?Convert.ToBase64String(value.SerializedValue as byte []) : string.Empty;

                        break;

                    default:
                        throw new NotImplementedException();
                    }
                }
            }
            if (hasChanges)
            {
                config.Save(ConfigurationSaveMode.Minimal, true);
            }
#else // original impl. - likely buggy to miss some properties to save
            foreach (ConfigurationSection configSection in userGroup.Sections)
            {
                ClientSettingsSection userSection = configSection as ClientSettingsSection;
                if (userSection != null)
                {
/*
 *                                      userSection.Settings.Clear();
 *
 *                                      foreach (SettingsPropertyValue propertyValue in collection)
 *                                      {
 *                                              if (propertyValue.IsDirty)
 *                                              {
 *                                                      SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
 *                                                      element.Value.ValueXml = new XmlDocument();
 *                                                      element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
 *                                                      userSection.Settings.Add(element);
 *                                              }
 *                                      }
 */
                    foreach (SettingElement element in userSection.Settings)
                    {
                        if (collection [element.Name] != null)
                        {
                            if (collection [element.Name].Property.Attributes.Contains(typeof(SettingsManageabilityAttribute)) != isRoaming)
                            {
                                continue;
                            }

                            element.SerializeAs             = SettingsSerializeAs.String;
                            element.Value.ValueXml.InnerXml = (string)collection [element.Name].SerializedValue;                                ///Value = XmlElement
                        }
                    }
                }
            }
            config.Save(ConfigurationSaveMode.Minimal, true);
#endif
        }
 /// <inheritdoc />
 public global::System.Configuration.Configuration OpenExeConfiguration(ConfigurationUserLevel pConfigurationUserLevel)
 {
     return ConfigurationManager.OpenExeConfiguration(pConfigurationUserLevel);
 }
Example #41
0
			public string GetSettingValue (ConfigurationUserLevel userLevel, string key)
			{
				global::System.Configuration.Configuration config =
					ConfigurationManager.OpenExeConfiguration (userLevel);
				KeyValueConfigurationElement value = config.AppSettings.Settings [key];
				return value != null ? value.Value : null;
			}
		private void SaveProperties (ExeConfigurationFileMap exeMap, SettingsPropertyValueCollection collection, ConfigurationUserLevel level, SettingsContext context, bool checkUserLevel)
		{
			Configuration config = ConfigurationManager.OpenMappedExeConfiguration (exeMap, level);
			
			UserSettingsGroup userGroup = config.GetSectionGroup ("userSettings") as UserSettingsGroup;
			bool isRoaming = (level == ConfigurationUserLevel.PerUserRoaming);

#if true // my reimplementation

			if (userGroup == null) {
				userGroup = new UserSettingsGroup ();
				config.SectionGroups.Add ("userSettings", userGroup);
				ApplicationSettingsBase asb = context.CurrentSettings;
				ClientSettingsSection cs = new ClientSettingsSection ();
				string class_name = NormalizeInvalidXmlChars ((asb != null ? asb.GetType () : typeof (ApplicationSettingsBase)).FullName);
				userGroup.Sections.Add (class_name, cs);
			}

			bool hasChanges = false;

			foreach (ConfigurationSection section in userGroup.Sections) {
				ClientSettingsSection userSection = section as ClientSettingsSection;
				if (userSection == null)
					continue;

				foreach (SettingsPropertyValue value in collection) {
					if (checkUserLevel && value.Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
						continue;
					// The default impl does not save the ApplicationScopedSetting properties
					if (value.Property.Attributes.Contains (typeof (ApplicationScopedSettingAttribute)))
						continue;

					hasChanges = true;
					SettingElement element = userSection.Settings.Get (value.Name);
					if (element == null) {
						element = new SettingElement (value.Name, value.Property.SerializeAs);
						userSection.Settings.Add (element);
					}
					if (element.Value.ValueXml == null)
						element.Value.ValueXml = new XmlDocument ().CreateElement ("value");
					switch (value.Property.SerializeAs) {
					case SettingsSerializeAs.Xml:
						element.Value.ValueXml.InnerXml = (value.SerializedValue as string) ?? string.Empty;
						break;
					case SettingsSerializeAs.String:
						element.Value.ValueXml.InnerText = value.SerializedValue as string;
						break;
					case SettingsSerializeAs.Binary:
						element.Value.ValueXml.InnerText = value.SerializedValue != null ? Convert.ToBase64String (value.SerializedValue as byte []) : string.Empty;
						break;
					default:
						throw new NotImplementedException ();
					}
				}
			}
			if (hasChanges)
				config.Save (ConfigurationSaveMode.Minimal, true);

#else // original impl. - likely buggy to miss some properties to save

			foreach (ConfigurationSection configSection in userGroup.Sections)
			{
				ClientSettingsSection userSection = configSection as ClientSettingsSection;
				if (userSection != null)
				{
/*
					userSection.Settings.Clear();

					foreach (SettingsPropertyValue propertyValue in collection)
					{
						if (propertyValue.IsDirty)
						{
							SettingElement element = new SettingElement(propertyValue.Name, SettingsSerializeAs.String);
							element.Value.ValueXml = new XmlDocument();
							element.Value.ValueXml.InnerXml = (string)propertyValue.SerializedValue;
							userSection.Settings.Add(element);
						}
					}
*/
					foreach (SettingElement element in userSection.Settings)
					{
						if (collection [element.Name] != null) {
							if (collection [element.Name].Property.Attributes.Contains (typeof (SettingsManageabilityAttribute)) != isRoaming)
								continue;

							element.SerializeAs = SettingsSerializeAs.String;
							element.Value.ValueXml.InnerXml = (string) collection [element.Name].SerializedValue;	///Value = XmlElement
						}
					}
 
				}
			}
			config.Save (ConfigurationSaveMode.Minimal, true);
#endif
		}
 public override void Init(IInternalConfigRoot root, params object[] hostInitParams)
 {
     _map   = (ExeConfigurationFileMap)hostInitParams[0];
     _level = (ConfigurationUserLevel)hostInitParams[1];
     CheckFileMap(_level, _map);
 }
Example #44
0
			public string GetFilePath (ConfigurationUserLevel userLevel)
			{
				global::System.Configuration.Configuration config =
					ConfigurationManager.OpenExeConfiguration (userLevel);
				return config.FilePath;
			}
Example #45
0
 internal static Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)
 {
     return(ConfigFactory.Create(typeof(ClientSettingsConfigurationHost), userLevel));
 }
		public override void Init (IInternalConfigRoot root, params object[] hostInitParams)
		{
			map = (ExeConfigurationFileMap) hostInitParams [0];
			level = (ConfigurationUserLevel) hostInitParams [1];
			CheckFileMap (level, map);
		}
Example #47
0
        private void LoadProperties(ExeConfigurationFileMap exeMap, SettingsPropertyCollection collection, ConfigurationUserLevel level, string sectionGroupName, bool allowOverwrite, string groupName)
        {
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, level);

            ConfigurationSectionGroup sectionGroup = config.GetSectionGroup(sectionGroupName);

            if (sectionGroup != null)
            {
                foreach (ConfigurationSection configSection in sectionGroup.Sections)
                {
                    if (configSection.SectionInformation.Name != groupName)
                    {
                        continue;
                    }

                    ClientSettingsSection clientSection = configSection as ClientSettingsSection;
                    if (clientSection == null)
                    {
                        continue;
                    }

                    foreach (SettingElement element in clientSection.Settings)
                    {
                        LoadPropertyValue(collection, element, allowOverwrite);
                    }
                    // Only the first one seems to be processed by MS
                    break;
                }
            }
        }
		public override void InitForConfiguration (ref string locationSubPath, out string configPath, out string locationConfigPath, IInternalConfigRoot root, params object[] hostInitConfigurationParams)
		{
			map = (ExeConfigurationFileMap) hostInitConfigurationParams [0];

			if (hostInitConfigurationParams.Length > 1 && 
			    hostInitConfigurationParams [1] is ConfigurationUserLevel)
				level = (ConfigurationUserLevel) hostInitConfigurationParams [1];

			CheckFileMap (level, map);

			if (locationSubPath == null)
				switch (level) {
				case ConfigurationUserLevel.PerUserRoaming:
					if (map.RoamingUserConfigFilename == null)
						throw new ArgumentException ("RoamingUserConfigFilename must be set correctly");
					locationSubPath = "roaming";
					break;
				case ConfigurationUserLevel.PerUserRoamingAndLocal:
					if (map.LocalUserConfigFilename == null)
						throw new ArgumentException ("LocalUserConfigFilename must be set correctly");
					locationSubPath = "local";
					break;
				}

			configPath = null;
			string next = null;

			locationConfigPath = null;

			if (locationSubPath == "exe" || locationSubPath == null && map.ExeConfigFilename != null) {
				configPath = "exe";
				next = "machine";
				locationConfigPath = map.ExeConfigFilename;
			}
			
			if (locationSubPath == "local" && map.LocalUserConfigFilename != null) {
				configPath = "local";
				next = "roaming";
				locationConfigPath = map.LocalUserConfigFilename;
			}
			
			if (locationSubPath == "roaming" && map.RoamingUserConfigFilename != null) {
				configPath = "roaming";
				next = "exe";
				locationConfigPath = map.RoamingUserConfigFilename;
			}
			
			if ((locationSubPath == "machine" || configPath == null) && map.MachineConfigFilename != null) {
				configPath = "machine";
				next = null;
			}
			locationSubPath = next;
		}
Example #49
0
 public static void SetConfigurationUserLevel(ConfigurationUserLevel configurationUserLevel)
 {
     _configurationUserLevel = configurationUserLevel;
 }