Change default config file name

O

olduncleamos

I am trying to share one config file between multiple console apps. Is
there anyway I can set the config file name for the current
application?

Thanks in advance.
 
M

Morten Wennevik [C# MVP]

olduncleamos said:
I am trying to share one config file between multiple console apps. Is
there anyway I can set the config file name for the current
application?

Thanks in advance.

Hi Amos,

You can't specify configuration name like you can do with the output name of
the assembly, but you can create your own configuration file, name it
whatever you want and load it with . The code below demonstrates how to do
this. Config.file was originally a regular app.config file which was renamed
and copied to another folder.

C:\config.file
---
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key" value ="Hello World"/>
</appSettings>
</configuration>



class Program
{
static void Main(string[] args)
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap();
map.ExeConfigFilename = @"C:\Config.file";

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

Console.WriteLine(config.AppSettings.Settings["Key"].Value);
}
}

Using this method you will probably lose all type safety benefits gained
through a Settings file though. There may be ways to tell a Settings file
that the underlying configuration file is not the application configuration
file, but I am not aware of this possibility.
 
D

Duggi

I am trying to share one config file between multiple console apps. Is
there anyway I can set the config file name for the current
application?

Thanks in advance.

Hello!!

Use the following line of code to change default config file

AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "c:/test.config");

use the same line of code in both the projects to set a common config
file for both the projects

Hope this helps.


-Cnu
 

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