
Loading Configuration files in C#
I don't think Daniel was speaking of the App.Config file. It seemed to me
that he was referring to custom configuration files. I have this problem. I
have some XML configuration files that I use, but they are accessed from a
stand-alone assembly. I can't create an App.Config for them, because they
are not an app. They are some common use assemblies that can be used by many
different programs (but not GAC-deployed), which I would like to be able to
have in 2 different locations on the same machine using different config
files local to that assembly. But try as I might, nothing I do (from that
assembly) can give me the path to the physical location of the assembly, so
I cannot access the configuration files or create subdirectories from that
path to log data relative to the assemblies. The ugly solution: Create
registry entries, which limits me to one set of configuration files and one
area for log files.
Does anyone know how this might be done? Seems like a simple request, but
it's been made quite difficult in .NET. Works for EXE's... but not for just
an assembly not associated with a specific EXE. In my case, this assembly
runs from ASP.NET apps, EXE's, SQL Server jobs (through a COM Wrapper). And
I want different configurations for all of them.
Help? Anyone? Haven't actually tried to work on this for a long time because
I don't have more time to waste on it. For now it's just doing the ugly
solution and I live with it, along with the rest of my team who use it.
Quote:
> > I have created a program that requires the loading of two
> > configuration files. I have included them in my project in the root
> > path. The problem is when I build the project it looks for them in
> > the current directory.
> Two config files? How do you load them?
> There is an API (RemotingConfiguration::Configure()) that lets you load a
> file _specifically_ for .NET remoting, but there is no such API for the
> application config file. The system will automatically load the config
file
> named after the application when the default app domain is created.
> > I have seen some people referencing the
> > location of the files using a format similar to
> > Solution.Project.filename
> If they do this, they are either 1) loading a config file for remoting or
2)
> they want to parse the file themselves and call appropriate (if available)
> APIs to make the settings
> > Can anyone point me in the direction of how to load these files
> > relative to my project rather than through actually directory names?
> The feature you are talking about is a VS.NET facilitiy for C# (I don't
know
> about VB.NET) the facility does not exist for C++ but can be simulated
with
> build events. Indeed, I guess this is how C# a project manages this, it
> specifically looks for a file called app.config, then it copies it to the
> same folder as the EXE you are building renaming the file after the EXE.
> If you want to have two config files in your project (but note only _one_
> can be used at runtime) you will need to use some build event to copy the
> appropriate file to the output directory. C# does not support build
events,
> but you can use the Makefile project (under the C++ category in the New
> Project wizard) and do this via an nmake makefile.
> Richard
> --
> Richard Grimes [MVP]
> author: "Programming with Managed Extensions for Visual C++ .NET",
> the Microsoft Press book about Managed C++