Newbie question: DLLs and Config files

G

Guest

I have used application configuration files with windows and web apps. Now I
would like to create a class library and associate a config file with the
DLL. This is to support directory changes that will take place in the
future. I want to put directory pathnames in the config file and read then
from the DLL methods. When I try to use an app.config with class library i
have problems. Any suggestions?
 
J

jdmartinez

I'm fairly certain that you cannot use a .config with a .dll. It only
works with executables (ie. you can't have mylib.dll.config). You're
just going to have to include the configuration settings the
executable's configuration file.

Joel Martinez
http://www.onetug.org - Orlando .NET User Group
http://www.codecube.net - blog
 
C

cecil

Alan,
What kind of problems are you having? A class library can
certainly have a config file and if it is not working for you no one
can help you with out knowing what the problem is.
cecil Howell MCSD, MCT
 
G

Guest

TY Cecil;

I have a class library in which I have some file manipulation and datbase
access methods. The objective is to install the library as XP on SQL Server
and it is greatly desired to have a config file so as to control some
pathnames and control varibales. If I include the Class library with a
project (say a win app), I can read read the App.Config file from the the
class library without any problems. If I try to use a app.config with class
library assembly, then code such as
ConfigurationSettings.AppSettings["ErrorLogFile"] no longer returns any
values. In fact, an exception is thrown if i try.

Is there some way to associate a config file with the class library
assembly? I looked at the documentation on customizing assemblies, but it is
not clear to me how to associate a config file with the class library
assembly.

Any help will be appreciated.

AlanS
 
C

cecil

Alan,
Your config file needs to be in the same folder as the dll was
loaded from and needs to be named identically to the dll ie dll =
util.dll config = util.dll.config

I could maybe tell you more but you did not specify what the exception
was that was thrown, you need to help us help you! If you are going to
post you need to start including specifics about what you are doing and
what is going wrong. You will find people will be quicker to help and
you will likely solve your problem if you include some details when you
post.

Cecil Howell MCSD, MCT

TY Cecil;

I have a class library in which I have some file manipulation and datbase
access methods. The objective is to install the library as XP on SQL Server
and it is greatly desired to have a config file so as to control some
pathnames and control varibales. If I include the Class library with a
project (say a win app), I can read read the App.Config file from the the
class library without any problems. If I try to use a app.config with class
library assembly, then code such as
ConfigurationSettings.AppSettings["ErrorLogFile"] no longer returns any
values. In fact, an exception is thrown if i try.

Is there some way to associate a config file with the class library
assembly? I looked at the documentation on customizing assemblies, but it is
not clear to me how to associate a config file with the class library
 
M

Marc Scheuner [MVP ADSI]

Your config file needs to be in the same folder as the dll was
loaded from and needs to be named identically to the dll ie dll =
util.dll config = util.dll.config

BUT: the .NET System.Configuration.ConfigurationSettings class will
*NOT* work on this .config file! You will *NOT* be able to retrieve
values from it!

The ConfigurationSettings class will *ONLY* work on the app's
"MyApp.exe.config" that is calling that particular DLL.

If you want to use a config file for your DLL, you'll need to "roll
your own" - there's quite a few samples of how to do this on e.g.
www.codeproject.com.

Marc
================================================================
Marc Scheuner May The Source Be With You!
Bern, Switzerland m.scheuner(at)inova.ch
 
R

Richard Grimes [MVP]

AlanS said:
XP on SQL Server and it is greatly desired to have a config file so
as to control some pathnames and control varibales. If I include the

If you use the framework config API this data must be in the app's config
file or in machine.config.
Class library with a project (say a win app), I can read read the
App.Config file from the the class library without any problems. If

That's the intended behaviour.
I try to use a app.config with class library assembly, then code such
as ConfigurationSettings.AppSettings["ErrorLogFile"] no longer
returns any values. In fact, an exception is thrown if i try.

Nope.

The reason is that the AppDomain is created with the name of the config
file, once the AppDomain is created you cannot change the name of the config
file. The default name of the config file is <app file name>.config.
However, if you create a new AppDomain you can tell it to use any config
file you specify, but I don't think that you want to do this!
Is there some way to associate a config file with the class library
assembly? I looked at the documentation on customizing assemblies,
but it is not clear to me how to associate a config file with the
class library assembly.

If all you want to do is to control "some pathnames and control varibales"
then you could create a class with these as public properties and then use
the XmlSerializer class to serialize/deserialize instances of this class to
an XML class. If you are doing this, then you store this file in isolated
storage if the data is associated with a user, or to the application's
folder if the data is not user specific.

Richard
 

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