Souvik Ghosh
Souvik Ghosh

Reputation: 4606

Refer current executing assembly config settings

I am trying to read the connection string value from the current executing assembly's config settings-

public static string ConnectionString
{
    get
    {
        string path = Assembly.GetExecutingAssembly().Location;

        Configuration config = ConfigurationManager.OpenExeConfiguration(path);

        return config.ConnectionStrings["DBCon"].ToString();
    }
}

I am getting below error which says-

'ConfigurationElement.this[ConfigurationProperty]' is inaccessible due to it's protection level

enter image description here

Any help on this?

Upvotes: 1

Views: 816

Answers (1)

spodger
spodger

Reputation: 1679

Try

return config.ConnectionStrings.ConnectionStrings["DBCon"].ToString();

config.ConnectionStrings returns a ConnectionStringsSection object which is public but whose elements [string]are internal. However ConnectionStringsSection has a public ConnectionStrings property which is a ConnectionStringSettingsCollection object whose elements [string] are public.

Upvotes: 1

Related Questions