Config file reading - c#

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

Related

How to read values from multiple application configuration files in c#?

I am having two different application configuration files in my project.
I need to read values from these two config files in my code. Searched a lot and found most of the answers are: use ConfigurationManager. But I can't read the second config file in my code. Please help on this.
Example:
1. app1.config
2. app2.config
how to read the value of the app2.config?
Never used the app.config in .NET but another solution would be using Xml(XmlReader, XmlWriter) from the System.Xml namespace and you can create a tag for Configuration and use as many configurations as you like, all this in one file. e.g.
<Configuration name="app1">
//here you would have your 1 configuration
</Configuration>
<Configuration name="app2">
//here you would have your 2 configuration
</Configuration>
The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project. Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.
You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder
You can use the ConfigurationManager Class to load an alternate config file by code.
Reference:
Handle multiple configuration files
Managing Multiple Configuration File Environments with Pre-Build Events
Atlast I found the way to handling multiple configuration files, the only way is use the SECTION handling within the main app.config file.
The best way is first understand .NET configuration. The best source is http://www.codeproject.com/Articles/16466/Unraveling-the-Mysteries-of-NET-2-0-Configuration
Really hard for the first time to understand, once you are clear with this idea you can do wonders using configuration files in .NET. This is the only solution I found throughout the internet.
Thank you,
Happy coding.

Does the appsettings file attribute override what is in the app.config?

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

Is there a way to have web.config values overwrite app.config values?

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.

Multiple App.Config Files in .NET Class library project

I am creating one class library project.
Now by default I have one App.Config file so that I am putting all environment specific data in that Config file.
Now based on the Environment (whether Dev / Test / Production), I am planning to have three App.Config files in VS 2010 such as
App.Dev.Config
App.Test.Config
App.Prod.Config
Wondering how would the application know which config file to use.
Anyone implemented this scenario. Any Code samples / Articles would be helpful.
Thanks
The app will use the config file named YourExcecutable.exe.config which is by default the file App.config included in your (executable) project.
Note, that .NET only loads one config file for the whole application. You cannot use multiple configuration files (i.e. one per library project) without coding.
Option: You can use postbuild events and different solution configurations to copy one or another App.Config file to the output folder
Option: You can use the ConfigurationManager Class to load an alternate config file by code.
Loading a different application configuration file at run time can be done using the concept of mapped configuration file. To start with, you need to add reference to System.Configuration.dll in your project.
Set the value of Copy to Output Directory property to Copy if newer (Refer screenshot). This has to be done only for non-default configuration files e.g. App1.config, App2.config, etc. Leave the default configuration file namely App.config as it is . Due to this change, all the non-default application configuration files will be available in the project output directory (\bin\debug) when the project is built. Default value of this property is Do not copy.
Here is the code snippet on how to read configuration data from non-default configuration files:
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = "App1.config"; // app1.config should be present in root directory from where application exe is kicked off
// Get the mapped configuration file
var config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
//get the relevant section from the config object
AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");
//get key value pair
var keyValueConfigElement = section.Settings["appSettingsKey"];
var appSettingsValue = keyValueConfigElement.Value;
If you have multiple application (aka app) configuration files then you can keep a setting in default App.config file with the help of which you can make a decision at run time about which non-default configuration file to load e.g. App1.config
Note: Now look at below code:
ConfigurationManager.AppSettings["DeployEnv"]
This code will still read the data from the default App.config file. This behavior can't be changed. There is no way to prohibit the Loading of default App.config file. You have to use alternate means as discussed in this post to read the data from non-default configuration files
Now there is an even better solution: SlowCheetah - XML Transforms

Adding a Custom Configuration file in C# application

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.

Categories

Resources