Solved

Windows Form App - Access App.Config file AppSettings

Posted on 2014-04-25
6
3,073 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
ID: 40023488
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
ID: 40023746
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
ID: 40024114
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
ID: 40024548
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
ID: 40033924
Thanks James :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
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!
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

948 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

22 Experts available now in Live!

Get 1:1 Help Now