Solved

c# configuration file

Posted on 2006-10-24
3
281 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 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now