473,320 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

ConfigurationSection and Class Library

Is it possible to load data from a configuration file within a assembly
(class library)? The hosting exe file is something completely different
which isn't gonna load my config file.

I already added a App.config to my project and its copied to my output
directory on build. Only it seems its not automatically loaded (is this
only possible with executables or asp.net?).

Normally you can call the configurationmanager to get a custom section
(or appsetting).
I use this to get a section.
ConfigurationManager.GetSection("samplesection"). Only this returns
null in my case. Appsetings is also empty.

I know its also possible with
ConfigurationManager.OpenMappedExeConfiguration and then giving it a
complete path to the config file. Only this requires a constant value
telling me the name of the configuration file which I preffer not. I
rather have it loaded automatically (if possible offcourse).

Any thoughts?

Apr 27 '06 #1
7 12997
Even the ConfigurationManager.OpenMappedExeConfiguration doesn't work
afterall. It's loading the machine config instead. Am I forgetting
something. I used the sample from MSDN:
http://msdn2.microsoft.com/en-us/lib...on(VS.80).aspx

Apr 27 '06 #2
Try renaming the configuration file to <executableName>.config

Apr 27 '06 #3
Then I get the following exception:

System.InvalidOperationException: An unexpected error occurred in
'ClientConfigurationHost::Init'.
at
System.Configuration.ClientConfigurationHost.Init( IInternalConfigRoot
configRoot, Object[] hostInitParams)

Configfile is named: <executableName>.exe.config
Code:

string path = Assembly.GetCallingAssembly().CodeBase;
string directoryPath = Path.GetDirectoryName(path);
string processName = Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(directoryPath, processName +
".exe.config");

ConfigurationManager.OpenMappedExeConfiguration(fi leMap,
ConfigurationUserLevel.None);

Apr 27 '06 #4
Hi,
"Alexander van Doormalen" <av*********@gmail.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
Is it possible to load data from a configuration file within a assembly
(class library)? The hosting exe file is something completely different
which isn't gonna load my config file.
Nop, a dll has no a config , you can check the archives for several threads
about this very same problem.

I already added a App.config to my project and its copied to my output
directory on build. Only it seems its not automatically loaded (is this
only possible with executables or asp.net?).
Yes, I think it's copied cause by default it's marked as content. the
framework will not use it at all.

Any thoughts?


You can use your own implementation of a config. you have two options if you
want to use a config file:
1- use a similar file that the one that .net use, this is a XML file that
you can easily parse, you can store the values in a hash table
2- use a text file (this is my preferred option) in the form key=value ,
read it and parse and either keep the values in a hashtable or store them in
predefined variables.
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Apr 27 '06 #5
Parsing it myself is 1 thing I don't wanna do anymore. The new
configuration options of .net 2.0 are great and they need to work.
Key=value method is old school (ini) and its not what I need.
ConfigSections is the way to go for my solution.

So still need a solution to fix the problem posted earlier and I'll be
happy

Apr 27 '06 #6
Fixed the exception. I think its a bug within the framework. Its caused
by the file:// (uri format) that Assembly.CodeBase returns.

Solution:

Uri path = new Uri(Assembly.GetCallingAssembly().CodeBase);
string directoryPath = Path.GetDirectoryName(path.LocalPath);
string processName = Process.GetCurrentProcess().ProcessName;

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Path.Combine(directoryPath, processName +
".exe.config");

ConfigurationManager.OpenMappedExeConfiguration(fi leMap,
ConfigurationUserLevel.None);

Anyways if someone knows how to autoload the Configuration that would
be even better. For now this will do I guess. Thnx!

Apr 27 '06 #7
Guess my problem isn't solved yet.

My second problem is that the config file now loads under the host's
appdomain. Any ConfigurationSection types defined in the config file
cannot be found. Why? because the host executable is somewhere else
located on the disk.

Files can't be put in the GAC because they are extensions which need to
be in a specific directory which the host monitors and loads from.

Is there an other way to let the runtime know where it needs to look?

Config part I'm talking about:

<section name="somename" type="namespace.classname, assemblyname,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

So when loading the configuration file it tries to load
'namespace.classname' from 'assemblyname' but cannot find the assembly.

Apr 27 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: syd | last post by:
In my project, I've got dozens of similar classes with hundreds of description variables in each. In my illustrative example below, I have a Library class that contains a list of Nation classes. ...
0
by: Jérôme Piquot | last post by:
Hi, When i want to save configuration settings i have a "ConfigurationSection properties cannot be edited when locked" Exception. StackTrace " at...
0
by: Liam | last post by:
I'm using VS 2005 and I have a custom ConfigurationSection class that works perfectly. However, when I reference it from my test project it works fine for all of the properties except when they...
0
by: tony | last post by:
Hello! I have one solution file that consist of three project. One project that build the exe file called A One project that build a user control dll. In this user control we have a class...
0
by: tony | last post by:
Hello! This is a rather long mail but it's a very interesting one. I hope you read it. I have tried several times to get an answer to this mail but I have not get any answer saying something...
4
by: Dabbler | last post by:
I'm trying to setup a email info config section like: <contactForm> <email> <add key="toaddress" value="he@hishost.com;" /> <add key="toaddress" value="she@herhost.com;" /> <add key="toaddress"...
0
by: Michael | last post by:
Hi, I would like to know if it is possible to use the ConfigurationSection class to modify user settings. I have created custom configuration classes that derive from ConfigurationSetion and...
2
by: Smithers | last post by:
I have a Windows Forms application that implements a plug-in architecture whereby required assemblies are identified and loaded dynamically. Here are the relevant classes: A = application =...
1
by: =?Utf-8?B?UGhpbGlw?= | last post by:
I am using the ConfigurationSection, ConfigurationElement, ConfigurationElementCollection base classes for purposes of reading a customized web.config Configuration section.....and I then use the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.