Loading Configuration files in C# 
Author Message
 Loading Configuration files in C#

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.  I have seen some people referencing the
location of the files using a  format similar to
Solution.Project.filename

Can anyone point me in the direction of how to load these files
relative to my project rather than through actually directory names?



Sat, 08 Jan 2005 01:18:14 GMT  
 Loading Configuration files in C#


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.

Quote:
> 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

Quote:
> 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++



Sat, 08 Jan 2005 20:30:25 GMT  
 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++



Sat, 08 Jan 2005 23:20:48 GMT  
 Loading Configuration files in C#

Quote:
> 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.

So the assembly is a library (DLL), yes?

Quote:
>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.

I don't understand. A library assembly is either shared (in the GAC, which
you say is not the case) or it is in the same folder as the process that
loads the library (or a subfolder of that folder). The subfolder could be
localised (eg en or en-US), the same name as the library, or a file
mentioned as the codebase in the app config file. The library cannot be in
any folder unrelated to the application.

From your explanation I deduce that you have a utility library assembly that
you share between several applications, you share it as a private assembly
(ie one copy of the utility assembly for each application). You want to have
an XML config file for the library and load it yourself. If this is the
case, why not just allow the system to load the XML file from the 'current
directory'?

Have you seen the code that Mathias Axelsson posted in this group

Mathias uses the approach of assuming that the config file is in the same
folder as the process, which is the logical place for the config file to be.

Richard



Mon, 10 Jan 2005 03:41:13 GMT  
 Loading Configuration files in C#
This article did not help as the "working directory" was in temporary
windows folders. However, using Assembly.CodeBase, I was able to get the
location to the original assembly, prior to JIT compilation, and it works.
The article did help me to look in to this again, and to find the solution,
so I thank you for that.



Quote:


> > 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.

> So the assembly is a library (DLL), yes?

> >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.

> I don't understand. A library assembly is either shared (in the GAC, which
> you say is not the case) or it is in the same folder as the process that
> loads the library (or a subfolder of that folder). The subfolder could be
> localised (eg en or en-US), the same name as the library, or a file
> mentioned as the codebase in the app config file. The library cannot be in
> any folder unrelated to the application.

> From your explanation I deduce that you have a utility library assembly
that
> you share between several applications, you share it as a private assembly
> (ie one copy of the utility assembly for each application). You want to
have
> an XML config file for the library and load it yourself. If this is the
> case, why not just allow the system to load the XML file from the 'current
> directory'?

> Have you seen the code that Mathias Axelsson posted in this group

> Mathias uses the approach of assuming that the config file is in the same
> folder as the process, which is the logical place for the config file to
be.

> Richard



Tue, 11 Jan 2005 02:03:56 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. component loads configuration file

2. how to: XML file as application configuration file

3. Newbie: separate big .cs file into small .cs files

4. Configuration Manager loses configurations

5. Unhandled Exception: System.Configuration.ConfigurationException: Could not create System Configuration.NameValueSectionHandler, System

6. saving configuration files

7. Project Configurations are out of date -- another solution for Libraries/LIB files

8. Configuration files for DLL`s when app is unmanaged .exe

9. fgets reading a configuration file: compiles OK but output wrong

10. fgets reading a configuration file:

11. reading a configuration file

12. Configuration file

 

 
Powered by phpBB® Forum Software