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?
ipjyoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
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.
grayeCommented:
It'd be like this:
xmlFile = Properties.Settings.Default.Xml
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

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"];
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.
ipjyoAuthor Commented:
The target framework is 3.5 if that makes any difference.

Thanks
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>

 
   
 
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>
ipjyoAuthor Commented:
This section was already there and generated automatically.

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

Thanks.
grayeCommented:
So, I take it that the simple solution of just using
Properties.Settings.Default.Xml
didn't work for you?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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"/>
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
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.