Solved

Windows Form App - Access App.Config file AppSettings

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 39
ASP.NET MVC 2 31
Need to start a web service from Visual Studio 2015 Pro 2 21
Problem!!! 8 33
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

786 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