1

How do I read connection strings from custom config file (say abc.config) using WebConfigurationManager from asp.net's C# code?

Configuration conf = WebConfigurationManager.OpenWebConfiguration("~/abc.config");

This doesn't seem to work.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Sam
  • 933
  • 5
  • 14
  • 26
  • What do you mean doesn't seem to work ? Do you get an exception ? Is it not loaded ? What is the bahvior ? – Jason De Oliveira Jun 15 '12 at 21:14
  • Check your `config path` properly. It should be simple. Is the `conf ` object null ? – Angshuman Agarwal Jun 15 '12 at 21:14
  • It reads some SQLEXPRESS as data source (a different connection string) not from abc.config – Sam Jun 15 '12 at 21:24
  • SQLEXPRESS was 0th element, the connection string from abc.config was the 1st element. – Sam Jun 15 '12 at 21:27
  • What do you mean by that ? This information seems irrelevant to the question. Can you please share the file ? You are telling its a custom config and **not** a standard `web.config` file. – Angshuman Agarwal Jun 15 '12 at 21:27
  • Sorry. First I tried as a custom config file as above. Then I created a dir and renamed the config web.config like Jason De Oliveira mentioned (this works). But, how can I read from a custom config file? – Sam Jun 15 '12 at 21:30
  • @Angshuman Agarwal, the abc.config has connectionStrings. I read connectionStrings from conf – Sam Jun 15 '12 at 21:48
  • @Sam - Probably look at this SO post - http://stackoverflow.com/q/6341906/763026 – Angshuman Agarwal Jun 15 '12 at 21:52

2 Answers2

3

you can use this trick: its my custom method- using webapp.config from web root. readl all app settings and return;

//Read WebAppConfiguration
public static AppSettingsSection ReadAllWebappConfig()
{
    string physicalWebAppPath = "";
    AppSettingsSection appSettings;

    ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    physicalWebAppPath = System.Web.Hosting.HostingEnvironment.MapPath("~/webapp.config");

    if (System.IO.File.Exists(physicalWebAppPath))
    {
        fileMap.ExeConfigFilename = physicalWebAppPath;
        Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
        appSettings = (AppSettingsSection)config.GetSection("appSettings");
    }
    else
        appSettings = null;

    return appSettings;
}

webapp.config sample:

<configuration>
  <appSettings>
    <add key="WebApp-FixedTopMenu" value="true"/>
    <add key="WebApp-FixedTopMenuThickness" value="true"/>
  </appSettings>
</configuration>
mRizvandi
  • 983
  • 1
  • 8
  • 20
  • What if I don't have appSettings node ? – Shesha Sep 21 '16 at 05:41
  • All XML need root and item element, its a standard XML, You need the root (configuration) and Item element (appSettings) and your desire properties (such as 'WebApp-FixedTopMenu'). – mRizvandi Oct 11 '16 at 14:00
1

I dont think you can read it with webconfigurationmanager. you will have read like any xml file as it is an xml file

public static string GetSingleValue(string strXPathExpression, string strAttributeName)
        {
            XmlNode node = GetNode(strXPathExpression);
            if (node != null)
            {
                XmlAttribute attribute = node.Attributes[strAttributeName];
                if (attribute != null)
                    return attribute.Value;
            }

            return string.Empty;


        }
Raab
  • 34,778
  • 4
  • 50
  • 65