How to use ConfigurationManager

173,832

Solution 1

Okay, it took me a while to see this, but there's no way this compiles:

return String.(ConfigurationManager.AppSettings[paramName]);

You're not even calling a method on the String type. Just do this:

return ConfigurationManager.AppSettings[paramName];

The AppSettings KeyValuePair already returns a string. If the name doesn't exist, it will return null.


Based on your edit you have not yet added a Reference to the System.Configuration assembly for the project you're working in.

Solution 2

Go to tools >> nuget >> console and type:

Install-Package System.Configuration.ConfigurationManager 

If you want a specific version:

Install-Package System.Configuration.ConfigurationManager -Version 4.5.0

Your ConfigurationManager dll will now be imported and the code will begin to work.

Share:
173,832
hbk
Author by

hbk

BOYCOTT on russia - don't buy, sell, support

Updated on October 13, 2021

Comments

  • hbk
    hbk over 2 years

    I want to use App.config for storing some setting. I tried to use the next code for getting a parameter from a config file.

    private string GetSettingValue(string paramName)
    {
        return String.Format(ConfigurationManager.AppSettings[paramName]);
    }
    

    I also added System.Configuration for it (I used a separate class), and in App.config file I have:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
      <appSettings>
        <add key ="key1" value ="Sample" />
      </appSettings>
    </configuration>
    

    But I got an error while trying to use ConfigurationManager - ConfigurationManager can't exist in such context, but I already added System.Configuration. Or did I miss something?

    EDIT:

    class with config (full view)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Configuration;
    
    namespace browser
    {
        class ConfigFile
        {
            private string GetSettingValue(string paramName)
            {
                return String.Format(ConfigurationManager.AppSettings[paramName]);
            }
        }
    }
    

    EDIT2

    Add how it looks

    enter image description here

    This means the problem is not during using ConfigurationManger but before - the program "says" that it "doesn't know such element" as I understand the error - the "Element ConfigurationManager" doesn't exist in such context"

    EDIT3

    enter image description here

    EDIT 4

    enter image description here

  • Nick_F
    Nick_F over 6 years
    Two users left negative ratings. It would be constructive to provide an indication why they consider the code is not good enough. I found out that if my application is located in C:\Program Files (x86)\MyApp, the code causes a crash, because the application does not have rights to save data in there.
  • Giulio Caccin
    Giulio Caccin over 4 years
    The original user problem is not about creating a method to save a missing configuration, it's about being unable to retrieve it because of a missing import. That's why this question is probably being downvoted: it's misleading.
  • tonyb
    tonyb over 3 years
    This method works great, but there is a problem. It saves the setting in a '.config' file in XML format alongside the compiled executable. If you build an installer MSI and a local administrator installs the application, the config file ends in a sub-folder the c:\program files... folder. The end user has no access to write to this folder, so an error occurs on any attempt to modify the config in this situation.