The appsettings tag in the app.config has a file attribute:
<appSettings file="other.config">
..
..
</appSettings>
How does this work? Will it merge what is in the appSettings (original) with the other.config file? Or will it overwrite it? What if the other.config file doesn't exist, should it crash?
I'm trying it myself and if a key isn't in the original, it doesn't seem to read it from the other.config?
Should the other.config file have just xml nodes, or should it all be inside a appsettings element?
<appSettings>
<userId>123</userId>
</appSettings>
or
<userId>123</userId>
If the file doesn't exist it will not crash, it will just be ignored.
The external config has to contain the <appSettings> node so your first example is correct.
The value in the external file will take priority, if no value is present then the app.config value is used.
Does that cover off everything?
One of the best answers on the subject is here: ASP.NET web.config: configSource vs. file attributes - Credit to #Massimiliano Peluso
file attribute
Specifies a relative path to an external file that contains custom application configuration settings
specific to the appSettings section
will merge (and override) settings in the .config file
will not cause web application to restart when modifying the specified file
http://msdn.microsoft.com/en-US/library/ms228154(v=vs.100).aspx
Using the Configuration.AppSettings.Settings.Add API will result in all settings being merged back into the main .config on a Configuration.Save call.
since .NET 1.1
Exception is not thrown if file does not exist.
configSource attribute
can apply to most sections of a configuration file, not just appSettings
will override the entire section with the external file, no merging
CAN cause web application to restart
http://msdn.microsoft.com/en-US/library/system.configuration.sectioninformation.configsource(v=vs.100).aspx
Using the Configuration.AppSettings.Settings.Add API will result in all settings being added to the file specified in configSource on a Configuration.Save call.
since .NET 2.0
System.Configuration.ConfigurationErrorsException is thrown if config source file does not exist.
The file attribute specifies an external file containing custom settings like you do in the appSettings entry of the web.config file.
Meanwhile, the external file specified in the configSource attribute contains the settings for the section which you declare the configSource for. For example, if you use the configSource attribute of the pages section, then the external file will contain the settings for the pages section.
The custom settings declared in the external config specifified in the
file attribute will be merged with the settings in the appSettings
section in the web.config file. In the meanwhile, the configSource
does not support merging, it means that you'll have to move the entire
section settings into the external file.
http://www.codeproject.com/Messages/1463547/Re-difference-between-configSource-and-file-attrib.aspx
Related
I have a web project that references a dll project that uses an app.config value. Is there a way to put the app.config values in the web.config so when the web project is run it pulls the values from the web.config? I've heard of this happening but I'm not 100% sure of it.
You can use the configSource attribute/property of each config element - this allows you to put sections of your configuration in an external file.
Once in an external file, you can reference this file from both your web.config and app.config files.
I dont think there is a wasy - but to open manually the app.config file ( if your start entry point is the web...)
so youll have to open each section and analze it.
I need to add configuration information to the assembly itself without including its app.config file with it.
How can i do it?
EDIT:
I need something like this
string config = #"<?xml version='1.0' encoding='utf-8'?>
<configuration>
.
.
.
.
</configuration>";
Set this hardcoded string configuration as current assembly configuration
Configuration settings be it user or application settings have their default values "hardcoded" in the assembly by default. They can be overriden by including an app.config, or modifying user settings at runtime and saving to the user config file.
After creating project settings (in project properties and go to the "Settings" tab), a Settings class is generated with static properties which will have the default value you configured.
They are accessible throughout your assembly like so:
Assert.AreEqual(Properties.Settings.MySetting, "MyDefaultValue");
These default values can be overridden via the app.config:
<applicationSettings>
<MyProject.Properties.Settings>
<setting name="MySetting" serializeAs="String">
<value>MyDefaultValue</value>
</setting>
</MyProject.Properties.Settings>
</applicationSettings>
To answer your question: You can omit including the app.config from your application deployment, the defaults that you provided when configuring the settings are hardcoded.
Edit:
Just noticed you actually want to read the entire app.config from your assembly.
A possible approach could be the following:
// 1. Create a temporary file
string fileName = Path.GetTempFileName();
// 2. Write the contents of your app.config to that file
File.WriteAllText(fileName, Properties.Settings.Default.DefaultConfiguration);
// 3. Set the default configuration file for this application to that file
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", fileName);
// 4. Refresh the sections you wish to reload
ConfigurationManager.RefreshSection("AppSettings");
ConfigurationManager.RefreshSection("connectionStrings");
// ...
you can have a custom XML file where you store your settings and then you set it as Embedded resource so it will be available inside the exe or dll.
see here: How can I retrieve an embedded xml resource? for an example on how to read it at runtime
Edit: and to load it as custom configuration file check here: Loading custom configuration files
I am trying to retrieve some information from a .config file. I need to search for specific tags and get the attributes in those tags. For example in application tag i need to get the attribute path. I don't need any other information from the .config file. I was just going through XmlTextReader but then realised i need a config file and don't know if it is the right way.
The right way is using a ConfigurationElement descendant. For each section in app.config / web.config you have a specific class to manage that section.
If you need to modify the app.config / web.config file, you can use the ConfigurationManager class.
I found Using the File attribute of the appSettings element of a .NET config file
And you must read this great article about using configuration file in .NET
Also found Read config file using XMl reader
If we can have more than one .config files, we can share one config file with other projects and put private configuration into another. Visual Studio 2008 will be confused?
No, except for the <appSettings> node which has a special file= attribute which works in a "cummulative" manner, all configuration sections are single shot affairs - you have it, and you have one of it exactly - or you have nothing.
<appSettings file="common.appsettings.config">
<add key="private1" value="value1" />
</appSettings>
This will read in the contents of the common.appsettings.config file and anything that's not being overwritten by an explicit value in your own config here is being used from that external config file.
You cannot add additional "private" info to an existing configuration section, as far as I know.
Visual Studio 2010 has support for multiple .config files. It is one feature of new web application packaging and deployment system. We can create now separate web.config files for each configuration we have for application. But for 2008 there are no support for multi config files, you can workaround this by adding two config files and rename them in build time
Example:
private.config
public.config
on pre-msbuild event merge these two files
rename merged file, it should be web.config or app.config
Hope this helps...
s
You can have multiple web config for each directory although only one config section handler.
The configuration files are hierarchical from general to specific. Configuration files further down in the hierarchy override any settings from the previous ones. So if you have a solution that includes a master web.config file, any web.config files in your projects override it and are specific and visible only to those projects. If a project is comprised of several folders, you can add a web.config file to each of those as well, again with different settings that also override any of those set above. But I caution over-engineering this or you may have difficulty with a trickle-down of certain property values or loopholes in security if it is security that you are configuring at each level.
Each config section can be put in an external file, and referenced from the main app.config / web.config via the configSource attribute. However, you will have to write custom section handlers for each custom section / external config file that you want to write, which may get a little tedious. In addition, the appSettings section can be externalised and referenced via the same mechanism.
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.
Is there any decent documentation that covers this topic?
thanks
You do not state what you want to achieve by having a separate file, but there are a couple of different things you can do.
If you want to "modularize" you configuration, you can break out certain config sections to separate files, using the configSource attribute:
// point out a file containing the connectionStrings config section
<connectionStrings configSource="connections.config"></connectionStrings>
You can also open a specific configuration file by calling ConfigurationManager.OpenExeConfiguration.