Solved

c# configuration file

Posted on 2006-10-24
3
290 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
[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
  • 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 250 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

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

705 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