Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the App.Config of another Exe

Tags:

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.

like image 881
Graviton Avatar asked Sep 10 '08 07:09

Graviton


People also ask

What is ExeConfigurationFileMap?

The ExeConfigurationFileMap class provides a means to define a custom configuration file hierarchy for .exe applications. The ExeConfigurationFileMap class can specify a single path or multiple paths related to configuration files.

Where are app config files stored?

The application configuration file usually lives in the same directory as your application. For web applications, it is named Web. config. For non-web applications, it starts life with the name of App.

Can we have multiple app config?

You cannot use multiple configuration files (i.e. one per library project) without coding.

Does app config get compiled?

Now you might be wondering what happens behind the scenes. Well, when you compile your application, the compiler actually copies the app. config file to the output folder, but gives it another name: When you start your application (ConsoleApp1.exe in our example), the matching config file will be loaded too.


2 Answers

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); 
like image 200
Espo Avatar answered Sep 18 '22 14:09

Espo


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.
like image 27
Graviton Avatar answered Sep 16 '22 14:09

Graviton