0

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?

2
  • Using app.config file should be trivial. Just in case.. is the app.config file on output folder? Not that you should manually do something to make this happen but let's start for the basic. Could you also paste how you were accessing the property and how was that property stored on app.config? Commented May 23, 2012 at 21:23
  • The config file does make its way to the bin under the name IndexLoader.dll.config. How the property is being stored in the config is shown in my question. I am not sure how to read the setting with the name ConnectionString. Commented May 23, 2012 at 21:29

1 Answer 1

5

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

Sign up to request clarification or add additional context in comments.

4 Comments

The code doesn't recognize properties. What is the namespace for Properties?
In your case it might be IndexLoader.IndexLoader.ConnectionString
are you having some kind of edit war over the correctness of: IndexLoader.IndexLoader.ConnectionString ?
@oers really don't know, I have just added a semicolon at the end. However, I'm waiting for the OP who has the last word on this. Its last comment here above is different from the last proposed edit. Waiting....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.