Solved

How to read appSettings section from App.Config?

Posted on 2010-08-16
13
887 Views
Last Modified: 2013-11-08
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
Comment
Question by:ipjyo
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 50 total points
ID: 33447326
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
 
LVL 18

Assisted Solution

by:Anil Golamari
Anil Golamari earned 50 total points
ID: 33447358
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
 
LVL 41

Assisted Solution

by:graye
graye earned 150 total points
ID: 33447371
It'd be like this:
xmlFile = Properties.Settings.Default.Xml
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 5

Assisted Solution

by:tvPrasad
tvPrasad earned 250 total points
ID: 33448513
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
 

Author Comment

by:ipjyo
ID: 33449107
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
 

Author Comment

by:ipjyo
ID: 33449115
The target framework is 3.5 if that makes any difference.

Thanks
0
 
LVL 5

Expert Comment

by:tvPrasad
ID: 33449386
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
 
LVL 5

Assisted Solution

by:tvPrasad
tvPrasad earned 250 total points
ID: 33449396
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
 

Author Comment

by:ipjyo
ID: 33449620
This section was already there and generated automatically.

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

Thanks.
0
 
LVL 41

Accepted Solution

by:
graye earned 150 total points
ID: 33449741
So, I take it that the simple solution of just using
Properties.Settings.Default.Xml
didn't work for you?
0
 
LVL 5

Assisted Solution

by:tvPrasad
tvPrasad earned 250 total points
ID: 33449862
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
 

Author Comment

by:ipjyo
ID: 33449997
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
 

Author Comment

by:ipjyo
ID: 33450097
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

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question