Andreas
Andreas

Reputation: 502

Get a List of non-inheritet connectionstrings in C#

I have an IIS (>7) with two applications /foo and /foo/bar. Both have several connectionstrings and I need to get a list of all connectionstrings of /foo/bar that are not inherited from /foo. I use System.Web.Configuration for some tasks and it would be cool to find a way to utilizing that classes here too. Any ideas?

I cannot read the web.config directly because the connectionStrings-section is protected.

Upvotes: 0

Views: 75

Answers (1)

Andreas
Andreas

Reputation: 502

Ok, I found an solution.

var config = WebConfigurationManager.OpenWebConfiguration(i_applicationVirtualPath);
if (config != null) {
    ConnectionStringsSection connectionStringSection = config.ConnectionStrings;
    ConnectionStringSettingsCollection connectionStringsCollection =
                connectionStringSection.ConnectionStrings;

    foreach (var settingObject in connectionStringsCollection) {
        ConnectionStringSettings setting = settingObject as ConnectionStringSettings;
        if (setting.ElementInformation.IsPresent) {
            // This connectionstring is in the corresponding web.config
        }
    }
}

This code detects only the connectionstrings I was looking for and is able to read transparently through the encryption. This should also work well for other configuration sections as well ...

Upvotes: 1

Related Questions