23

I have an exe with an App.Config file. Now I want to create a wrapper dll around the exe in order to consume some of the functionalities.

The question is how can I access the app.config property in the exe from the wrapper dll?

Maybe I should be a little bit more in my questions, I have the following app.config content with the exe:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="myKey" value="myValue"/>
  </appSettings>
</configuration>

The question is how to how to get "myValue" out from the wrapper dll?


thanks for your solution.

Actually my initial concept was to avoid XML file reading method or LINQ or whatever. My preferred solution was to use the configuration manager libraries and the like.

I'll appreciate any help that uses the classes that are normally associated with accessing app.config properties.

Graviton
  • 81,782
  • 146
  • 424
  • 602

6 Answers6

24

The ConfigurationManager.OpenMappedExeConfiguration Method will allow you to do this.

Sample from the MSDN page:

static void GetMappedExeConfigurationSections()
{
    // Get the machine.config file.
    ExeConfigurationFileMap fileMap =
        new ExeConfigurationFileMap();
    // You may want to map to your own exe.comfig file here.
    fileMap.ExeConfigFilename = 
        @"C:\test\ConfigurationManager.exe.config";
    System.Configuration.Configuration config =
        ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
        ConfigurationUserLevel.None);

    // Loop to get the sections. Display basic information.
    Console.WriteLine("Name, Allow Definition");
    int i = 0;
    foreach (ConfigurationSection section in config.Sections)
    {
        Console.WriteLine(
            section.SectionInformation.Name + "\t" +
        section.SectionInformation.AllowExeDefinition);
        i += 1;

    }
    Console.WriteLine("[Total number of sections: {0}]", i);

    // Display machine.config path.
    Console.WriteLine("[File path: {0}]", config.FilePath);
}

EDIT: This should output the "myKey" value:

ExeConfigurationFileMap fileMap =
    new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = 
    @"C:\test\ConfigurationManager.exe.config";
System.Configuration.Configuration config =
    ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
    ConfigurationUserLevel.None);
Console.WriteLine(config.AppSettings.Settings["MyKey"].Value);
stevethethread
  • 2,524
  • 2
  • 30
  • 29
Espo
  • 41,399
  • 21
  • 132
  • 159
  • Hi Espo, your suggestion was great. But maybe what I was looking for was a bit different. I have edited my question to be more explicit. – Graviton Sep 10 '08 at 08:09
  • I have now added a sample of how you can output your required key/value. – Espo Sep 10 '08 at 19:22
6

After some testing, I found a way to do this.

  1. Add the App.Config file to the test project. Use "Add as a link" option.
  2. Use System.Configuration.ConfigurationManager.AppSettings["myKey"] to access the value.
Graviton
  • 81,782
  • 146
  • 424
  • 602
  • +1 Would have posted this answer if you hadn't already. This is also a good solution to some other scenarios regarding config files spread across different projects within one solution. – takrl Feb 23 '12 at 07:29
  • Here's how to [add as link](https://andrewlock.net/including-linked-files-from-outside-the-project-directory-in-asp-net-core/) – Jesus is Lord Apr 10 '18 at 15:51
4

I think what you're looking for is:

System.Configuration.ConfigurationManager.OpenExeConfiguration(string path)
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
lomaxx
  • 113,627
  • 57
  • 144
  • 179
0

I'd second Gishu's point that there's another way. Wouldn't it be better to abstact the common/"public" part of the EXE out into DLL create a wrapper EXE to run it? This is certainly the more usual pattern of development. Only the stuff that you wish to consume would go into the DLL, and the EXE would do all the stuff it currently does, minus what's gone into the DLL.

alastairs
  • 6,697
  • 8
  • 50
  • 64
  • Hi alastairs, I realize that you have a point here. But I am creating the wrapper dll as a test assembly to test the logic inside my exe. Therefore I have no choice :) – Graviton Sep 10 '08 at 07:28
-1

It's an xml file, you can use Linq-XML or DOM based approaches to parse out the relevant information.
(that said I'd question if there isn't a better design for whatever it is.. you're trying to achieve.)

Gishu
  • 134,492
  • 47
  • 225
  • 308
-1

Adding a link in the IDE would only help during development. I think lomaxx has the right idea: System.Configuration.ConfigurationManager.OpenExeConfiguration.

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
tkrehbiel
  • 750
  • 1
  • 6
  • 9