Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to read appSettings section from App.Config?

Posted on 2010-08-16
13
Medium Priority
?
900 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 200 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 200 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 600 total points
ID: 33447371
It'd be like this:
xmlFile = Properties.Settings.Default.Xml
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 5

Assisted Solution

by:tvPrasad
tvPrasad earned 1000 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 1000 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 600 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 1000 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

Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

885 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