How to modify/update custom config section of app.config using c# - c#

I am trying to save some configurations in app.config for my windows application.
I searched in Google for solution but not anything related to this. My requirement is I want to save/update the configurations in app.config using my windows application.
My schema will be like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="customAppSettings1" type="System.Configuration.NameValueSectionHandler"/>
<section name="customAppSettings2" type="System.Configuration.NameValueSectionHandler"/>
</configSections>
<customAppSettings1>
<add key="FirstKey" value="1" />
<add key="SecondKey" value="2" />
</customAppSettings1>
<customAppSettings2>
<add key="FirstKey" value="1" />
<add key="SecondKey" value="2" />
</customAppSettings2>
</configuration>

This code is giving changed custom values. but not saving on physical file. A bit progress in your question.
var xmlDoc = new XmlDocument();
xmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
xmlDoc.SelectSingleNode("//applicationSettings/MyApp_Application.Properties.Settings/setting/value").InnerText = "server=127.0.0.1;uid=root;pwd=root;database=" + comboBox1.SelectedItem.ToString();
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
ConfigurationManager.RefreshSection("applicationSettings/MyApp_Application.Properties.Settings/setting");

Related

cant read custom section from web config

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

App.config can't find my element

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.
:)

Get winform Webbrowsercontrol Source from app config file

Can someone please show me how to get winforms Webbrowsercontrol source from appconfig file ?.
I tried with following but it's not working
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
and this is my how app config file look like
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="serverip" value="127.0.0.1" />
<add key="dbport" value="3306" />
<add key="defdatabase" value="waq115" />
<add key="dateandtime" value="http://free.timeanddate.com/clock/i4daxch9/n77/fs18/fcfff/tc212426/pc212426" />
</appSettings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>
Navigate() is a method not a variable, So the following line of code will not compile,
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
You have to read the URL from the application configuration file and pass it as an argument to Navigate("URL") method as follows.
webBrowser1.Navigate(ConfigurationManager.AppSettings["dateandtime"]);

Is the config file name "quartz.config" fixed (means hard-code, cannot be changed)?

I know that Quartz.Net has several ways to loads it configuration upon startup: (from http://jvilalta.blogspot.com/2011/03/how-does-quartznet-configuration-work.html )
The hosting application’s configuration file
A file specified in an environment variable
The quartz.config file
The embedded configuration file
Notice the quartz.config file.
My question:
Is the config file name "quartz.config" fixed (means hard-code,
cannot be changed)?
If no, how can I change it? e.g. I want to read from
FinancialQuartz.config instead of quartz.config.
If no option to change the name "quartz.config". How can I specify
when to read from FinancialQuartz.config or
CalculationQuartz.config? (I have no real scenario for this
question, just wonder)
Regards,
I'm confused.
In DotNet. You can point the app.config or web.config file to your file of choice, like below.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="quartz" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<quartz configSource="MyQuartzSettings.config" />
And "MyQuartzSettings.config" looks something like this (one example of many)
<quartz>
<add key="quartz.plugin.jobInitializer.type" value="Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin" />
<add key="quartz.scheduler.instanceName" value="DefaultQuartzScheduler" />
<add key="quartz.threadPool.type" value="Quartz.Simpl.SimpleThreadPool, Quartz" />
<add key="quartz.threadPool.threadCount" value="10" />
<add key="quartz.threadPool.threadPriority" value="2" />
<add key="quartz.jobStore.misfireThreshold" value="60000" />
<add key="quartz.jobStore.type" value="Quartz.Simpl.RAMJobStore, Quartz" />
<add key="quartz.plugin.jobInitializer.fileNames" value="Quartz_Jobs_001.xml" />
<add key="quartz.plugin.jobInitializer.failOnFileNotFound" value="true" />
<add key="quartz.plugin.jobInitializer.scanInterval" value="120" />
</quartz>
Is that what you're talking about?
In addition to the answer given by #granadaCoder, you can also set the quartz.config environment variable to the name of the file you want to load. Note that the configSource attribute is not quartz.net specific but a feature of the .Net framework: SectionInformation.ConfigSource Property
I think it's fixed now. But there's a workaround for it.
Read the configuration from other ways into a System.Collections.Specialized.NameValueCollection.
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddIniFile("someotherfile.ini", false, true);
var config = builder.Build();
var quartzProperties = new System.Collections.Specialized.NameValueCollection();
foreach (var p in config.GetSection("Quartz").GetChildren().AsEnumerable())
{
quartzProperties.Add(p.Key, p.Value);
}
IScheduler scheduler = new StdSchedulerFactory(quartzProperties).GetScheduler();
scheduler.Start();

Split EnterpriseLibrary Block configurations into multiple files

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.

Categories