Solved

Windows Form App - Access App.Config file AppSettings

Posted on 2014-04-25
6
3,108 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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