Techno_Dex
Wed Jun 14 14:09:04 CDT 2006
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>
"Dmytro Lapshyn [MVP]" <x-code@no-spam-please.hotpop.com> wrote in message
news:Of%23awD4jGHA.4512@TK2MSFTNGP02.phx.gbl...
> 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.
>
> "Techno_Dex" <nospamchurst@osi-corp.com> wrote in message
> news:OoRTaWxjGHA.4884@TK2MSFTNGP03.phx.gbl...
>>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???
>>
>>
>