On 7/9/2020 6:58 AM, Anton Shepelev wrote:
> Do you know of a way to bypass app.config for such
> problems as are generally solved by its modifica-
> tion, e.g.:
>
>
https://www.spyriadis.net/2010/06/crystal-reports-framework-4-crdb_adoplus-dll/
> (Banner alert!)
>
> I for one hate these config files, written in cum-
> bersome XML and integrated into the framework so
> tightly that no code may be located and removed that
> loads those files. The first thing that I do while
> creating a new project is amputatate app.config. I
> could tolerate an clean .ini file, but not XML.
>
> It is very strange that .NET seems to insist on ev-
> eryone's reliance on those config files without pro-
> vision of alternative ways to achieve the same ef-
> fect from code. Or am I wrong?
I think a program app.config is way better than hiding the
config in the registry.
And XML is a lot more flexible than INI.
I don't think it is supported to override default
config mechanism.
But it is doable.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="somekey" value="somevalue"/>
</appSettings>
</configuration>
using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Configuration.Internal;
using System.Reflection;
namespace E
{
public class MyConfig : IInternalConfigSystem
{
public NameValueCollection appset;
public MyConfig()
{
appset = new NameValueCollection();
appset.Add("somekey", "someothervalue"); // this could come from
another file or from a database or from wherever
}
public object GetSection(string key)
{
if(key == "appSettings")
{
return appset;
}
else
{
return null;
}
}
public void RefreshConfig(string sectionName)
{
}
public bool SupportsUserConfig { get; private set; }
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ConfigurationManager.AppSettings["somekey"]);
Type type = typeof(ConfigurationManager);
FieldInfo info = type.GetField("s_configSystem",
BindingFlags.NonPublic | BindingFlags.Static);
info.SetValue(null, new MyConfig());
Console.WriteLine(ConfigurationManager.AppSettings["somekey"]);
}
}
}
somevalue
someothervalue
Arne