App.config use from UserControl Assembly

T

Techno_Dex

I have an assembly (dll) which contains home rolled C# UI controls that are
used in multiple applications in house. Some of these UI controls I would
like to have read some settings from the currently running application
*.config file but with the new .NET 2.0 configuration changes I'm seeing how
this is possible. I looked into creating an ApplicationSettingBase class
which would contain my custom settings configurations, but when trying to
create the properties which would be used to load it, I discovered that I
can't access the <Assembly>.Properties namespace since my assembly is not an
exe. I don't want to hard code an assembly namespace in either as that
would defeat the purpose of having a UI Controls assembly. Is there any way
in the .NET 2.0 structure to have say a third party dll which contains user
controls which are used in an application and can read from that
application's *.config file? What am I missing???
 
D

Dmytro Lapshyn [MVP]

Hi,

The easiest way would be to use the Assembly.GetEntryAssembly() method to
get the main executable assembly to query its properties.

As a more complex and flexible solution, consider using the Configuration
Application Block from the Microsoft Patterns and Practices website
(http://msdn.microsoft.com/practices/) - now seems to be a part of
Enterprise Library for .NET 2.0, this one is listed in Top Downloads on the
website's home page.
 
T

Techno_Dex

Both of these are some of the thoughts that came to mind, but I still have
problems with them. The first one I'm not sure how to get at the properties
namespace to extract the values (if they even exist), but think it would
require me to hard code information about the assemblies which I would like
to avoid (esp if causing circular references). The second one is a little
overkill at this point. From looking at the EnterpriseLib code, it appears
that they are opening the config file and modifying it during runtime which
was the whole point of MS reworking the architecture in .NET 2.0 so that
this wouldn't occur. I can't believe that when they rearchitected this that
they would hose things up this bad.
 
T

Techno_Dex

I figured out how to do this with some manual intervention. Each project in
..NET 2.0 can have it's own app.config file (but it isn't copied to the build
directory). If you add the desired parameters using the Project -->
Properties --> Settings functionality to the dll, then copy the settings
into the exe's app.config file then the dll will have access to the
settings. What happens is the Params are embedded in the Properties
Namespace in a file called Settings.settings. Then they can be accessed in
the assembly by calling Properties.Settings.Default.<Param_Name> to get it's
value. When running an exe, the dll's internal variables are read first,
but if you include the values (and change them) in the exe's app.config
file, then they are overridden at run time. This functionality only appears
to work when calling it from the Properties Namespace. Trying to use the
ConfigurationManager.AppSettings.Get() doesn't work.

Here is a sample app.config file with the combined EXE and DLL app.config
file parts. Note you must add in the <configSections> part also, not just
the <applicationSettings>
<?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="<EXE_ProductNamespace>.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
<section name="<DLL_ProductNamespace>.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"
/>
</sectionGroup>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="<EXE_ProductNamespace>.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<section name="<DLL_ProductNamespace>.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<<EXE_ProductNamespace>.Properties.Settings>
<setting name="ExeParam1" serializeAs="String">
<value>DisplayThisText</value>
</setting>
</<EXE_ProductNamespace>.Properties.Settings>
<<DLL_ProductNamespace>.Properties.Settings>
<setting name="DLLParam1" serializeAs="String">
<value>DisplayThisTextInDll</value>
</setting>
</<DLL_ProductNamespace>.Properties.Settings>
</applicationSettings>
<userSettings>
<<EXE_ProductNamespace>.Properties.Settings>
<setting name="ExeUserParam1" serializeAs="String">
<value>DisplayThisText</value>
</setting>
</<EXE_ProductNamespace>.Properties.Settings>
<<DLL_ProductNamespace>.Properties.Settings>
<setting name="DLLUserParam1" serializeAs="String">
<value>DisplayThisTextInDll</value>
</setting>
</<DLL_ProductNamespace>.Properties.Settings>
</userSettings>
</configuration>
 

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