Custom Configuration Files...

G

Guest

Is it possible to create a custom configuration file (other than app.config)
that can be processed by classes in the System.Configuration namespace?

I have seen a ton of articles that talk about custom sections (inside the
app.config file) but I would like to make an entirely new config file. I
opened a custom config file with the ConfigurationManager class but it seemd
as if it ignored all of the content in the config file.

Is there any decent documentation that covers this topic?

Thank you,

Jason Richmeier
 
G

Greg Young

For any app domains that you create you can use
AppDomainSetup.ConfigurationFile but since you do not control the creation
of your default application domain you cannot change the name. A thought
might be that you could spawn off a secondary domain from the main domain
(changing the name of the config)

Another idea would be to use ConfigurationManager.OpenExeConfiguration(file)
http://msdn2.microsoft.com/en-us/library/ms224437.aspx which would return
you a seperate Configuration object for the file.

Cheers,

Greg Young
MVP - C#
 
G

Guest

The overload of the OpenExeConfiguration method that you mentioned is the one
that I am using.

Here is what I did yesterday evening. In order to get something to work
quickly, I downloaded the sample application from the current issue of MSDN
magazine (about configuration files). I replaced the line of code

config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)

with

config = ConfigurationManager.OpenExeConfiguration("d:\test.config")

I have a file named test.config with the exact same contents as the
app.config in the application located a "d:\test.config". When I run the
application, the custom sections are ignored. When I run the application
with the original line of code, the custom sections are processed.

Is there something I am doing wrong with this?

Thank you,

Jason Richmeier
 
Joined
Jul 19, 2006
Messages
1
Reaction score
0
Re: Custom configuration files

=?Utf-8?B?SmFzb24gUmljaG1laWVy?= said:
The overload of the OpenExeConfiguration method that you mentioned is the one
that I am using.
(...)

config = ConfigurationManager.OpenExeConfiguration("d:\test.config")

Is there something I am doing wrong with this?
I'm trying to do this exact same thing. I realised (and confirmed) that when you do this, the configuration manager is actually reading a file called "d:\test.config.config". In my case I tried passing "c:\temp\myconfig.xml" and then in debug I queried for config.FilePath and of course it was "c:\temp\myconfig.xml.config". This is obvious in hindsight.

My next idea was to use this the OpenMappedExeConfiguration() method instead, because it seemed like you can speciiy the config file directly:

ExeConfigurationFileMap cfgMap = new ExeConfigurationFileMap();
cfgMap.ExeConfigFilename = "c:\temp\myconfig.xml" ;

System.Configuration.Configuration config ;
config = ConfigurationManager.OpenMappedExeConfiguration(
cfgMap,
ConfigurationUserLevel.None );

This works in that the config.FilePath reports the correct file, but I am having trouble with reading the custom sections out of it!

I'm not the only one:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=14962&SiteID=1

I will post back here if I get any further. I may have to resort to parsing the files manually.
 
Last edited:

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