Exemple #1
0
        bool IsValidSection(string configFile, string configSection)
        {
            var exeMap = new ExeConfigurationFileMap();

            exeMap.ExeConfigFilename = configFile;

            global::System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(exeMap, ConfigurationUserLevel.None);
            if (config == null)
            {
                return(false);
            }

            try
            {
                var cs = config.GetSection(configSection) as ConfigurationSection;
                if (cs == null)
                {
                    return(false);
                }
            }
            catch (CryptographicException)
            {
                // Ignore - it's ok, since we only want to know if the section is
                // valid and this exception is thrown when an attempt to decrypt a
                // section is made and the protection provider for some reason
                // didn't do the job (e.g. we encrypted the section with local store
                // key container and the provider wants to access a global store one
                // and cannot find it)
            }

            return(true);
        }
            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);
            }
Exemple #3
0
        //------------------------------------------------------------------------------
        // Get collection of connections from provided configuration
        //------------------------------------------------------------------------------
        private DBConnectionList GetConfigConnections(Configuration configuration)
        {
            DBConnectionList configConnections = null;

            if (configuration != null)
            {
                ConnectionStringSettingsList connectionStringSettingsList = GetConnectionStringSettingsList(configuration);
                if (connectionStringSettingsList != null)
                {
                    foreach (ConnectionStringSettings connectionStringSettings in connectionStringSettingsList)
                    {
                        DBConnection configConnection = new ConfigConnection(ServiceProvider, connectionStringSettings);
                        if (configConnections == null)
                        {
                            configConnections = new DBConnectionList();
                        }
                        configConnections.Add(configConnection);
                    }
                }
            }
            return(configConnections);
        }
Exemple #4
0
        //------------------------------------------------------------------------------
        // Gets a list of all the web specific connection string settings, filtering out parent settings
        //------------------------------------------------------------------------------
        private ConnectionStringSettingsList GetConnectionStringSettingsList(Configuration configuration)
        {
            if (configuration == null)
            {
                return(null);
            }

            ConnectionStringSettingsList connectionStringSettingsList = new ConnectionStringSettingsList();

            try
            {
                // Get the connection string settings from config
                ConnectionStringsSection           connectionStringsSection           = (ConnectionStringsSection)configuration.GetSection("connectionStrings");
                ConnectionStringSettingsCollection connectionStringSettingsCollection = connectionStringsSection.ConnectionStrings;

                // Get the parent connection string settings from config
                ConnectionStringsSection           parentConnectionStringsSection           = (ConnectionStringsSection)connectionStringsSection.SectionInformation.GetParentSection();
                ConnectionStringSettingsCollection parentConnectionStringSettingsCollection = parentConnectionStringsSection.ConnectionStrings;

                // Filter out parent connection string settings
                foreach (ConnectionStringSettings connectionStringSettings in connectionStringSettingsCollection)
                {
                    if (string.IsNullOrEmpty(connectionStringSettings.Name) ||
                        parentConnectionStringSettingsCollection[connectionStringSettings.Name] == null)
                    {
                        connectionStringSettingsList.Add(connectionStringSettings);
                    }
                }
            }
            catch (Exception)
            {
                //If web.config file is in bad shape, configuration API will throw an exception. We should silently eat the exception and display the message
                connectionStringSettingsList = null;
            }
            return(connectionStringSettingsList);
        }
 public string GetSettingValue(string exePath, string key)
 {
     global::System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(exePath);
     return(config.AppSettings.Settings [key].Value);
 }
 public string GetFilePath(ConfigurationUserLevel userLevel)
 {
     global::System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(userLevel);
     return(config.FilePath);
 }
 public string GetFilePath(string exePath)
 {
     global::System.Configuration.Configuration config =
         ConfigurationManager.OpenExeConfiguration(exePath);
     return(config.FilePath);
 }
Exemple #8
0
 //------------------------------------------------------------------------------
 // Get writable collection of connections from web.config
 //------------------------------------------------------------------------------
 private DBConnectionList GetConfigConnections(out Configuration configuration)
 {
     configuration = OpenWebConfiguration(false /*readOnly*/);
     return(GetConfigConnections(configuration));
 }