3

This is my first time using an XML config file. In the solution explorer I right click, add, new item, application config. file.

I then edited the file to read...

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>    
    <add key="Key1" value="1000" />    
  </appSettings>
</configuration>

...and tried to access it with...

Int32.Parse(ConfigurationManager.AppSettings.Get("Key1"));

...but it says that the parameter is null. I looked at just ConfigurationManager.AppSettings and it's AllKeys property has dimension 0.

What am I doing wrong?

Thanks!

3 Answers 3

2
  1. Right-click on your project, choose Properties>Settings tab and edit your settings there because the config file is not the only place these values are stored at.

  2. In your code use Settings.Default.MySetting to access the settings.

You'll need to add using MyProjectName.Properties; to your using statements in order to access the settings this way or you'll have to fully qualify it as MyProjectName.Properties.Settings.Default.MySetting...

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

1 Comment

Any reason onto why ConfigurationManager.AppSettings cant be used?
1

I know this is super old but I just encountered this opening an old project. A reference to System.Configuration was missing.

Comments

0

To ensure get information from App.config using ConfigurationManager.AppSettings["<Key name>"]; be sure to set your values inside <appSettings> section.

Example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />      
    </startup>
  
   <appSettings>
    <add key ="IntervalSeconds" value ="60000"/>
    <add key ="OutputPathLog" value ="\\myserver\hackingtrack\Projects\Log\"/>
    <!--Jorgesys si Elenasys sunt puisori-->
    <add key="InputFeeds1" value="https://cld.blahbla.com//portadaKick.json" />
    <add key="InputFeeds2" value="https://cld.blahbla.com//portadaKick.json" />
    <add key="InputFeeds3" value="https://cld.blahbla.com//portadaKick.json" />

    </appSettings>
</configuration>

Retrieving values:

   string intervalSeconds = ConfigurationManager.AppSettings["IntervalSeconds"];
   string inputFeeds1 = ConfigurationManager.AppSettings["InputFeeds1"]; ; 
   string inputFeeds2 = ConfigurationManager.AppSettings["InputFeeds2"]; 
   string inputFeeds3 = ConfigurationManager.AppSettings["InputFeeds3"]; 

Comments

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.