Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
212 views
in Technique[技术] by (71.8m points)

c# - Get the App.Config of another Exe

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:estConfigurationManager.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 + "" +
        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:estConfigurationManager.exe.config";
System.Configuration.Configuration config =
    ConfigurationManager.OpenMappedExeConfiguration(fileMap, 
    ConfigurationUserLevel.None);
Console.WriteLine(config.AppSettings.Settings["MyKey"].Value);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...