Solved

Windows Form App - Access App.Config file AppSettings

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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…

615 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