• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 912
  • Last Modified:

How to read appSettings section from App.Config?

hi,

I have the below App.Config file in my project.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="Xml" value="C:\Files\ins.xml"/>
  </appSettings>
</configuration>

I am trying to get the value of key "Xml" as follows.

string xmlFile  = "";
xmlFile = ConfigurationSettings.AppSettings["Xml"];

But it is not showing the value when I tried to show the value in a messagebox.
I also imported the namespace "System.Collections.Specialized" still not working.

Any ideas please?
0
ipjyo
Asked:
ipjyo
  • 5
  • 4
  • 2
  • +2
7 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
Are you getting an error, or is it just not returning a value? Have you imported the System.Configuration namespace?

On a side note, the System.Collections.Specialized namespace isn't required because it isn't related to config settings.
0
 
Anil GolamariCommented:
System.Configuration.Configuration rootWebConfig1 =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
      if (rootWebConfig1.AppSettings.Settings.Count > 0)
      {
      System.Configuration.KeyValueConfigurationElement customSetting =
      rootWebConfig1.AppSettings.Settings["Xml"];
            if (Dml!= null)
               Console.WriteLine("customsetting1 application string = \"{0}\"",                                     Xml.Value);
      else
      Console.WriteLine("Could not find xml value in config file. ");
      }

Can you try this.

Good Luck.
0
 
grayeCommented:
It'd be like this:
xmlFile = Properties.Settings.Default.Xml
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
tvPrasadCommented:
Step 1# Add System.configuration to your references
Step 2# Add below line to  your class
using System.Configuration;
Step 3# Add below line to get config values
string xmlname= System.Configuration.ConfigurationManager.AppSettings["Xml"];
0
 
ipjyoAuthor Commented:
I imported System.Configuration namespace.
and then I tried the below statement and still not working. It is not giving any error message.
System.Configuration.ConfigurationManager.AppSettings["Xml"];


Thanks.
0
 
ipjyoAuthor Commented:
The target framework is 3.5 if that makes any difference.

Thanks
0
 
tvPrasadCommented:
Change the as below and try.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="XmlFileName" value="C:\Files\ins.xml"/>
  </appSettings>
</configuration>

 
   
 
0
 
tvPrasadCommented:
Change the as below and try.

 <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="XmlFileName" value="C:\Files\ins.xml"/>
  </appSettings>
0
 
ipjyoAuthor Commented:
This section was already there and generated automatically.

<?xml version="1.0" encoding="utf-8" ?>

Thanks.
0
 
grayeCommented:
So, I take it that the simple solution of just using
Properties.Settings.Default.Xml
didn't work for you?
0
 
tvPrasadCommented:
Oops!! lit looks like my editing messaged.

I mean to say just renamed the element name as below

<add key="XmlFileName" value="C:\Files\ins.xml"/>
0
 
ipjyoAuthor Commented:
I tried

Properties.Settings.Default.Xml

but it is saying "The name 'Properties' does not exist in the current context". Also when I tried to type the above line, visual studio is not giving intellisense for the above line. I think I need to add some reference.

Thanks
0
 
ipjyoAuthor Commented:
It is working when I created a new project and tried the code.

It must have been something with some other code in my project.
sorry for the trouble.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 5
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now