Question

When I just had an app.config, everytime I tried ConfigurationManager.AppSettings[0] or ConfigurationManager.AppSettings["keyName"] I got a null. So I tried to use a *.settings file which created me an app.config that looked like this

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
      <section name="IndexLoader.IndexLoader" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <applicationSettings>
    <IndexLoader.IndexLoader>
      <setting name="ConnectionString" serializeAs="String">
        <value>INITIAL CATALOG=xxx;DATA SOURCE=xxx;User ID=xxx;Password=xxx;Application Name=xxx;</value>
      </setting>
    </IndexLoader.IndexLoader>
  </applicationSettings>
</configuration>

Now how do I read this connection string using C#?

The solution has multiple projects and many of them have config files. Is there a way to specify that I want the config file in code's project to be used?

Was it helpful?

Solution

applicationSettings is not appSettings. To read an entry in applicationSettings you use

string myCnStr = Properties.Settings.Default.ConnectionString;

In your case it will be

string myCnStr =IndexLoader.IndexLoader.Default.ConnectionString;

However the ConnectionString has a dedicated section in the app.config and should be stored there and read from the ConfigurationManager.ConnectionStrings collection

See here for references

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top