Ejemplo n.º 1
0
		internal FullTrustConfigReaderWriter(string configFilePath)
		{
			if (!_isConfigLoaded)
			{
				if (string.IsNullOrEmpty(configFilePath))
				{
					_config = WebConfigurationManager.OpenWebConfiguration("~");
					_isWebConfig = true;
				}
				else
				{
					if (!File.Exists(configFilePath))
						throw new ConfigurationException(null, "The XML config file {0} could not be found", configFilePath);

					if (configFilePath.ToLower() == "app.config")
					{
						_config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
					}
					else
					{
						if (!File.Exists(configFilePath))
							throw new FileNotFoundException(string.Format("The config file {0} could not be found", configFilePath));

						ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
						fileMap.ExeConfigFilename = configFilePath;
						_config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
					}

					_isWebConfig = false;
				}

				ConfigFilePath = configFilePath;
			}

			// If there's no Roadkill section, ConfigFileManager is in an invalid state
			_section = _config.GetSection("roadkill") as RoadkillSection;
			if (_section == null)
			{
				string errorMessage = "";

				if (_isWebConfig)
					errorMessage = "The web.config file does not contain a Roadkill section";
				else
					errorMessage = string.Format("The config file{0} does not contain a Roadkill section", ConfigFilePath);

				throw new InvalidOperationException(errorMessage);
			}
		}