Solved

Windows Form App - Access App.Config file AppSettings

Posted on 2014-04-25
6
3,059 Views
Last Modified: 2014-04-30
Hello Experts,

I am trying to create Windows Form Application in Visual Studio 2012.

I have defined few key value pairs in AppSettings section of App.Config file something like below:

  <appSettings>
    <add key="GetSqlQueryPath" value="E:\MacysReflexHelper\SqlQuery\" />
  </appSettings>

Open in new window


I am trying to fetch those values from C# code like below:

string filePath = System.Configuration.ConfigurationManager.AppSettings["GetSqlQueryPath"].ToString();

string filePath = System.Configuration.ConfigurationSettings.AppSettings["GetSqlQueryPath"].ToString();

Open in new window



But none of the above has worked when I am trying to use first one ConfigurationManager, i am getting this error:

Error	2	The type or namespace name 'ConfigurationManager' does not exist in the namespace 'System.Configuration' (are you missing an assembly reference?)	c:\users\mkshah\documents\visual studio 2012\Projects\MacysNetProcessEmails\MacysNetProcessEmails\Business Classes\DatabaseConnection.cs	27	60	MacysNetProcessEmails

Open in new window


and when i am using second code i.e. ConfigurationSettings, i am getting this error:

Warning	1	'System.Configuration.ConfigurationSettings.AppSettings' is obsolete: 'This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.AppSettings'	c:\users\mkshah\documents\visual studio 2012\Projects\MacysNetProcessEmails\MacysNetProcessEmails\Business Classes\FileOperations.cs	27	31	MacysNetProcessEmails

Open in new window


Please let me know how I can access Configuration values for Windows Form Application.

Cheers...
0
Comment
Question by:Md Shah
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
Comment Utility
You'll need to add a reference to the System.Configuration to use the ConfigurationManager. It isn't included by default.
0
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
Comment Utility
If you added the settings through the Settings tab of the project's Properties window, the designer has automatically generated a class that you can use to access the setting with the following syntax:

<Application main class>.Settings.<Name of the setting>
YourApp.Settings.GetSqlQueryPath

The setting is ReadOnly if defined as an Application setting, ReadWrite and individual per user if defined as a User setting.

After changing a user settings, you will need to Save the settings in order to keep the change:

YourApp.Settings.Save
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Thanks Carl..

After adding the reference it has worked for me :)

Thanks to you also James for teaching me a new thing today, i was not aware of this till now.

However, I guess u missed 'Default' in hurry as this is working for me - 'YourApp.Settings.Default.GetSqlQueryPath'

Overall.. Thank You Experts !!
0
 
LVL 8

Author Comment

by:Md Shah
Comment Utility
Hey.. Small Correction I need to make here for this question's answer..

Carl's answer has resolved the current issue but when I use the Windows Forms App's reference in Windows Service, its not fetching the values from App.Config and throwing 'Object Reference doesn't exist' exception is thrown.

But James answer is works fine in all scenarios even when I use its reference in Windows Service project.

I wish I can change the points allocation now. Sorry for that James :(
0
 
LVL 8

Author Closing Comment

by:Md Shah
Comment Utility
Thanks James :)
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now