App.config in a Windows .NET application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am developing a windows .NET apps to be used in several countries with
different languages and I want to use a configuration file to retrieve the
fields in the right language depend on the selected country.

I saw that on a web apps but I am not sure if I can do the same on a win apps.

If someone have an idea of how to do that or give me an example about it, it
would be really appreciated

Thanks!

Sean.
 
Just create a App.config in the root of the project directory and include it
in the project.

J
 
I am developing a windows .NET apps to be used in several countries with
different languages and I want to use a configuration file to retrieve the
fields in the right language depend on the selected country.
I saw that on a web apps but I am not sure if I can do the same on a win apps.

Add a straight text file called "app.config" to your project - call it
just that - nothing else. It will be copied to your compile output
directory and renamed to "myapp.exe.config" by the VS.NET IDE for you.

The app.config file has a clear structure - you can read up about it
on MSDN. Basically, you can either use just the standard <appSettings>
section, to store a few select entries, or you can define your own
custom configuration section (of standard type), or you can even write
your own custom configuration section handler to handle specific
configuration settings.

The info in the app.config file is XML - you need to adhere to all XML
rules and stuff.

Here's a sample app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Default Language" value="en-US" />
<add key="FieldNames" value="fieldnames.txt" />
</appSettings>
</configuration>

To read those values, add the "System.Configuration" namespace to your
using clause, and then use

string sDefaultLanguage = ConfigurationSettings.AppSettings["Default
Language"];
string sFieldNames =
ConfigurationSettings.AppSettings["FieldNames"]


As I said - I would keep the number of entries low - a few, a few
dozen at most - not more.

Marc


================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Hi Marc

Your info is very useful ... but I have a question: from your example how
can I read the data from your .txt file? This is that I need for my apps.

Thanks for your reply ...

Sean


Marc Scheuner said:
I am developing a windows .NET apps to be used in several countries with
different languages and I want to use a configuration file to retrieve the
fields in the right language depend on the selected country.
I saw that on a web apps but I am not sure if I can do the same on a win apps.

Add a straight text file called "app.config" to your project - call it
just that - nothing else. It will be copied to your compile output
directory and renamed to "myapp.exe.config" by the VS.NET IDE for you.

The app.config file has a clear structure - you can read up about it
on MSDN. Basically, you can either use just the standard <appSettings>
section, to store a few select entries, or you can define your own
custom configuration section (of standard type), or you can even write
your own custom configuration section handler to handle specific
configuration settings.

The info in the app.config file is XML - you need to adhere to all XML
rules and stuff.

Here's a sample app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Default Language" value="en-US" />
<add key="FieldNames" value="fieldnames.txt" />
</appSettings>
</configuration>

To read those values, add the "System.Configuration" namespace to your
using clause, and then use

string sDefaultLanguage = ConfigurationSettings.AppSettings["Default
Language"];
string sFieldNames =
ConfigurationSettings.AppSettings["FieldNames"]


As I said - I would keep the number of entries low - a few, a few
dozen at most - not more.

Marc


================================================================
Marc Scheuner May The Source Be With You!
Berne, Switzerland m.scheuner -at- inova.ch
 
Your info is very useful ... but I have a question: from your example how
can I read the data from your .txt file? This is that I need for my apps.

You'd have to use the usual System.IO methods - open a file stream,
create a stream reader, read the file - there's no "magic" in the
config system to make that happen for you, sorry.

Marc
 
I added an apps.config file into my project but my application cannot read
the values.

You need to call the file "app.config" - *NOT* "apps.config" !!
Actually I have 3 project in my solution and I added this apps.config into
my data layer project to have several connection strings depending on what
data source using I am.

The app.config works *ONLY* with an EXE project - a class library
project creating a DLL assembly will *NOT* use any app.config files
(and it won't copy and rename them, either).

Marc
 
Back
Top