[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

c# configuration file

Posted on 2006-10-24
3
Medium Priority
?
295 Views
Last Modified: 2008-02-01
I have a c# program that I want to store configurations in a file and have my program read it. What would a coding look like for something like this?
0
Comment
Question by:bman9111
  • 2
3 Comments
 
LVL 13

Expert Comment

by:joechina
ID: 17797089
Which version of .net are you using?

Here is a simple example:
// C#
NameValueCollection AllAppSettings = ConfigurationManager.AppSettings;
Console.WriteLine(AllAppSettings["ApplicationName"]);

Put the following in your App.Config file
<appSettings>
<add key="ApplicationName" value="Demo Application"/>
<add key="ApplicationVersion" value="2.0.0.1"/>
<add key="UserFirstName" value="John"/>
<add key="UserLastName" value="Public"/>
</appSettings>

More detail please see System.configuration namespace

http://msdn2.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

The other option is to use new project settings feature in VS2005. (VS generates a set of class to manage settings)
Example
http://www.codeproject.com/vb/net/appsettings2005.asp
0
 
LVL 8

Author Comment

by:bman9111
ID: 17797272
I am using 2005.

Please explain how I can do this. What I want is a stand alone file that is in the same directory as the exe so that I could go into and change the settings versus having to go into the code and recompile.

0
 
LVL 13

Accepted Solution

by:
joechina earned 1000 total points
ID: 17797655
Right click your project and click Add --> new item...
From the list, select Application Configuration File.
Now you will have a App.config file in your project. Open it, and add
<appSettings>
     <add key="YourKey" value="Your Key Value"/>
</appSettings>
With <configuration> item.

Right click references in your project and click Add reference,
select System.configuration from the .NET page.

In you code, add
using System.Configuration;

Now in the code, you can use

ConfigurationManager.AppSettings["YourKey"] to get the value "Your Key Value".

Good Luck
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

867 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