Solved

How to read appSettings section from App.Config?

Posted on 2010-08-16
13
890 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

691 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