App.config file

  • Thread starter msnews.microsoft.com
  • Start date
M

msnews.microsoft.com

I am having a heck of a time trying to figure out how to get a setting in a
referenced assembly/dll from the exe's app.config file. In other words, I
have an assembly called AcmeApp.exe which contains an app.config file. This
exe references a dll AcmeHelper.dll which I am trying to read a
configuration setting out of the app.config file but am unable to get it to
work properly. I am using the
ConfigurationManager.AppSettings.Get("CustomSetting") to get the value from
the app.config (in C# .NET 2.0) Does anyone have any input on what I am
doing wrong. (FYI: my dll and exe have different namespaces) I have
included what would seam logical to me for the app.config.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="AcmeApp.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
<section name="AcmeHelper.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
</configSections>
<applicationSettings>
<AcmeApp.Properties.Settings>
<setting name="Test_Val1" serializeAs="String">
<value>AcmeApp_Title</value>
</setting>
</AcmeApp.Properties.Settings>
<AcmeHelper.Properties.Settings>
<setting name="CustomSetting" serializeAs="String">
<value>My Favorite Custom Setting</value>
</setting>
</AcmeHelper.Properties.Settings>
</applicationSettings>
</configuration>
 
T

Techno_Dex

The Settings class that gets created, is embedded in the dll, which means it
gets hard coded at build time. The idea behind a config file is that is can
be changed without having to rebuild assemblies every time. This is not a
viable solution as each custom will ultimately have different settings and
require the flexibility to change the default values.
 
F

FUnky

Techno_Dex said:
The Settings class that gets created, is embedded in the dll, which means
it gets hard coded at build time. The idea behind a config file is that
is can be changed without having to rebuild assemblies every time. This
is not a viable solution as each custom will ultimately have different
settings and require the flexibility to change the default values.

App.config is meant to be read-only and it contains your project's default
settings. The question pertained to this particular file .. so the solution
was perfectly correct.
In case someone wishes to have user specific custom settings, VS 2005
provides excellent solution for the same. It stores the user specific
settings in a separate user.config file under the User\Local
Settings\Application Data folder. When you run the exe, it first looks into
the user.config file. If it gets the settings, OK, but if not, it looks into
the app.config file and picks up the default settings.
 
T

Techno_Dex

That is so very backwards, as the settings may be application wide per
client, not user specific within the client's company. Say you have the
company's Name or a ServerName as a setting in the config file so it is
customized per client. The user.config file doesn't get created until the
first time the application runs and tries to write a value to the user
config section. This means they don't have the application customized until
the user launches the app for the first time not to mention the possiblity
that the app won't work without the required config info being set before
first launch. Besides, the App.config file is meant to be read-only at
run-time, which this is not as the values are set the first time after
installation and used from then on. Not to mention if you include user
scoped settings from the Properties->Settings form that they are created in
the app.config file in a <userSettings> group. Besides, if this were the
case, there would be absolutely no reason to use this file as opposed to
using a resource (.resx) file which does the same exact thing. This is
still not a viable solution as the values are being read from the
Settings.settings file not the actual app.config file. If this were the
case, then there would be no reason to even deploy the app.config file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top