开发者

How do I change the name of a default .config file in a .net project

I have a .net Console Application called FooConsole. When I build and deploy it, I see that the App.config file from my project is deployed to FooConsole.exe.config.

How can I deploy the .config file to Foo.config instead of FooConsole.exe.config and still have it read as the default .config f开发者_C百科ile?

UPDATE: I realize this sounds like an arbitrary requirement. I wanted to be able to write scripts that would copy custom .config files for different deployments. However, if my .config file name must depend on the name of the .exe, then if someone (likely) decides to change the name of the .exe file, it will break my scripts.

From your answers, it sounds like this is an issue I will simply have to work around.


Whilst you can't change what the default config file is, you can tell this config file to pull its setting from another file. e.g.

<configuration>
    <appSettings file="MyOther.config"></appSettings> 
</configuration>

http://msdn.microsoft.com/en-us/library/ms228154.aspx


The better question is: why do you want to? What's wrong with the default that bothers you?

That said, you can programmatically load a configuration file if you like, and you can name it whatever you like. This answer to a similar question links to all you need to know.


You cannot. The .NET configuration system will always try to find (exe file name).config - for your foo.exe, .NET will always look for foo.exe.config. There's no switch or setting or something to change that.

If you really really want to deviate from this convention (really?), then you'll need to roll your own using the Configuration class from the System.Configuration namespace.

If you really must change the config name (again: really? And why?), then use this code:

 ExeConfigurationFileMap map = new ExeConfigurationFileMap();
 map.ExeConfigFilename = "Custom.config";  // give it the name you want

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

 string value = custom.AppSettings.Settings["key"].Value;

You need to add "custom.config" to your project, and make sure it gets "copied to output directory if newer".

This works - you can basically load any file you want - but it's a bit of a kludge nonetheless...


Actually assuming you wantt to have a central configuration - the solution is simple: DONT USEE THE DEFAULT CONFIGURATION FILE FOR THAT... ...but roll your own mechanism. Reading a XML file is not that hard.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜