I have a simple app.config file that doesn't quite pass the IDEs checker.
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="XmlRoot" type="System.String"/>
</configSections>
<connectionStrings>
<omitted/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<XmlRoot>
<add key="relativepath" value=""/>
</XmlRoot>
</configuration>
I added the <section name="XmlRoot" type="System.String" /> part to the config and then I tried to define the name and key here: <add key="relativepath" value=""/>. But for some reason the IDE gives me the following Messages:
I rarely use the app.config file so it could just be a noob mistake. How do I make it recognize my tags?
If it's just a simple string, you can use AppSettings to accomplish this.
<configuration>
<appSettings>
<add key="relativepath" value="mypath" />
</appSettings>
</configuration>
Access it from C# like this:
string path = ConfigurationManager.AppSettings["relativepath"]);
Try changing your type to System.Configuration.NameValueSectionHandler
this is how I define my sections:
<configSections>
<sectionGroup name="MySettings">
<section name="serverConfiguration" type="System.Configuration.NameValueSectionHandler"></section>
<section name="sqlSettings" type="System.Configuration.NameValueSectionHandler"></section>
</sectionGroup>
</configSections>
...
<MySettings>
<sqlSettings>
<add key="sqlTransactionTimeOut" value="0" />
</sqlSettings>
<serverConfiguration>
<add key="resolveDnsAsync" value="true" />
</serverConfiguration>
</MySettings>
Probably you need to use the section appSettings
<configuration>
<appSettings>
<!-- and Here you define the key and the value like you do-->
<add key="relativepath" value="path"/>
</appSettings>
</configuration>
then in your code write something like this to read it.
String path = ConfigurationManager.AppSettings["relativepath"].ToString();
hope it works for you.
:)
Related
I want to add multiple int values to my Settings.
So far I have this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<DigiPortServer1>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</DigiPortServer1>
</configuration>
Is this right? I have found many questions considerung multiple String values. How can I access these values? I would like to save these into a int array or something similar.
I would modify your file to be in this way. So it will be easier to access and to work with. This way you can easily access those features by writing ConfigurationManager.AppSettings["3"].ToString();
Note that your file wasn´t having the keys associated with any values. I added the value atribute.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="DigiPortServer1"
type="Configuration.Helpers.MultipleValuesSelection, Configuration.Helpers"
requirePermission="false"/>
</configSections>
<appSettings>
<add key="3" value="3"></add>
<add key="4" value="4"></add>
<add key="5" value="5"></add>
<add key="6" value="6"></add>
<add key="7" value="7"></add>
<add key="8" value="8"></add>
<add key="9" value="9"></add>
<add key="10" value="10"></add>
<add key="11" value="11"></add>
<add key="12" value="12"></add>
<add key="13" value="13"></add>
<add key="14" value="14"></add>
<add key="15" value="15"></add>
<add key="16" value="16"></add>
<add key="17" value="17"></add>
<add key="18" value="18"></add>
</appSettings>
</configuration>
You could access the configuration section value using the ConfigurationManager GetSection method.
var section = System.Configuration.ConfigurationManager.GetSection("DigiPortServer1") as System.Collections.Specialized.NameValueCollection;
var value = section["keyname"];
If section is the name value pair then cast with above type (NameValueCollection) or you can use own type to cast.
I have a simple config section as follows
<configuration>
<configSections>
<section name="Test" type="System.Configuration.DictionarySectionHandler"/>
<section name="Test1" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<Test>
<add key="foo" value="1"/>
</Test>
<Test1>
<add key="bar" value="20"/>
</Test1>
</configuration>
And I am accessing it from code as
var blah = ConfigurationManager.GetSection("Test");
But I always get null. I tried everything but could not figure out whats going on. Could someone please help me with this?
Thanks,
Why can't I read this from my web config? When I used App.config it worked fine..
FundGroups = null
private static readonly NameValueCollection FundGroups =
(NameValueCollection)ConfigurationManager.GetSection("FundGroups");
Web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="FundGroups" type="System.Configuration.NameValueSectionHandler" />
</configSections>
<FundGroups>
<add key="Group1" value="Value1" />
<add key="Group2" value="Value2" />
<add key="Group3" value="Value3" />
<add key="Group4" value="Value4" />
<add key="Group5" value="Value5" />
<add key="Group6" value="Value6" />
<add key="Group7" value="Value7" />
</FundGroups>
</configuration>
I found a possible duplication here. But the ConfigurationSettings.GetConfig seems to be obsolete and doesnt work for me
In my config section I have the following:
<configuration>
<configSections>
<section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog"/>
<section name="NFeColumns" type="System.Configuration.DictionarySectionHandler" />
<section name="CTeColumns" type="System.Configuration.DictionarySectionHandler" />
</configSections>
<appSettings>
<add key="csv.separator" value=";" />
<add key="infNFe.columns" value="NFeColumns"/>
<add key="infCte.columns" value="CTeColumns"/>
<add key="infNFe.filename" value=".\Extracted\NFeCollection.csv"/>
<add key="infCte.filename" value=".\Extracted\CTeCollection.csv"/>
</appSettings>
<NFeColumns>
<add key="infNFe.Id" value="Id" />
<add key="ide.cUF" value="cUF" />
<add key="dest.CNPJ" value="CNPJ" />
<add key="dest.xNome" value="xNome" />
<add key="det.nItem" value="nItem" />
<add key="prod.cProd" value="cProd" />
<add key="prod.xProd" value="xProd" />
<add key="IPI.cEnq" value="cEnq" />
</NFeColumns>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I want to catch the values from and put in a IDictionary in the same order as the configuration file. I`m doing this:
string columnsSectionName = ConfigurationManager.AppSettings[colFileName + ".columns"];
IDictionary columns = (IDictionary)ConfigurationManager.GetSection(columnsSectionName);
However, Im geting the <NFeColumns> in a totally different order, and I cant figure out why. Can you please help me?
ConfigurationManager.GetSection returns an instance of HashTable. Items in a hash table won't be stored in the order they are inserted. You will have to manually order them.
Is it possible to split the configuration for an enterprise library block into multiple files?
For example: I have three assemblies and one hosting project. I want to store the entlib v6 Exception Handling Application Block (EHAB) configuration for each assembly in a separate config file located in the particular assembly.
The hosting project references the three assemblies:
assembly_X
ehabX.config
assembly_Y
ehabY.config
assembly_Z
ehabZ.config
Hosting project
using ehabX.config
using ehabY.config
using ehabZ.config
I already tried the following:
App.config in the hosting project:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="enterpriseLibrary.ConfigurationSource" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ConfigurationSourceSection, Microsoft.Practices.EnterpriseLibrary.Common" requirePermission="true" />
</configSections>
<enterpriseLibrary.ConfigurationSource>
<sources>
<add name="ehabX" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabX.config" />
<add name="ehabY" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabY.config" />
<add name="ehabZ" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.FileConfigurationSource, Microsoft.Practices.EnterpriseLibrary.Common" filePath="ExceptionHandling\ehabZ.config" />
</sources>
<redirectSections>
<add sourceName="ehabX" name="exceptionHandling" />
<add sourceName="ehabY" name="exceptionHandling" />
<add sourceName="ehabZ" name="exceptionHandling" />
</redirectSections>
</enterpriseLibrary.ConfigurationSource>
</configuration>
ehabX.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow NotImplementedException">
<exceptionTypes>
<add type="System.NotImplementedException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
ehabY.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling"/>
</configSections>
<exceptionHandling>
<exceptionPolicies>
<add name="Swallow ArgumentException">
<exceptionTypes>
<add type="System.ArgumentException, mscorlib" postHandlingAction="None" name="NotImplementedException"/>
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
The ehabZ.config is omitted.
Using the EHAB with the policy "Swallow NotImplementedException" works fine. But if I try to use the policy "Swallow ArgumentException" defnied in ehabY.config I get this error message:
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionHandlingException: The policy with name 'Swallow ArgumentException' cannot be found. Exception handling aborted.
Any suggestions?
Unfortunately, there is nothing out of the box that will let you merge multiple configuration sources into one configuration set.
I think you can probably do what you want but you will have to do some coding. You'll need to read in the appropriate configuration sources and create a custom IConfigurationSource that will merge them all. Additive merge of different config sources? has an example of a MergeConfigurationSource that could help you.