N
N
Nikita Moshkalov2016-05-06 00:39:02
C++ / C#
Nikita Moshkalov, 2016-05-06 00:39:02

How to process configuration file in C# DLL?

The task is to put some lines into the configuration file of the plugin implemented in the DLL. I’ll make a reservation right away that this is a plugin for someone else’s system and I don’t have access to the config because the sandbox for the plugin.
If the values ​​are entered as defaults, then the query seems to Properties.Settings.Default.passwdproduce the desired result. However, if the values ​​are unknown before compilation, we will get an error.
After a few hours of reading the internet, solution #1 was discovered:

private void ReadAllSettings()
    private void ReadAllSettings()
    {
        System.Configuration.Configuration config = null;
        string configPath = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
        ExeConfigurationFileMap map = new ExeConfigurationFileMap();
        map.ExeConfigFilename = configPath;
        Log(configPath);
        try
        {
            config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        }

        catch (Exception ex)
        {
            Log(ex.Message);
        }

        if (config != null)
        {
            Log("NOT NULL");
            Log(GetAppSetting(config, "passwd").Length.ToString());
        }
    }

As a result, I get 0.
Solution #2 is very similar, but uses OpenExeConfiguration instead of OpenExeConfiguration with a path. Sometimes ".config" is added. This has also been tested. The paths have been verified. I get something like this:
An error occurred loading a configuration file: The parameter 'exePath' is invalid.
Parameter name: exePath

In one of the tips I saw a clause about rights. So the hastily assembled ConsoleApp in the same folder shows the value changed by hand in the configuration file, and the removal of the configuration file to the ill-fated DLL will change a little less than nothing.
I really hope for help, because this plug, to put it mildly, is already annoying =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dvig8xi, 2016-05-06
@dvig8xi

I have like this

dllConfig = ConfigurationManager.OpenExeConfiguration(this.GetType().Assembly.Location);
connection = dllConfig.ConnectionStrings.ConnectionStrings[1].ConnectionString;

#
#algooptimize #bottize, 2016-05-09
@user004

OpenMappedExeConfiguration
should be the path to config here, not exe
OpenExeConfiguration here to exe

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question