【问题标题】:Get the App.Config of another Exe获取另一个exe的App.Config
【发布时间】:2010-09-08 09:18:07
【问题描述】:

我有一个带有App.Config 文件的exe。现在我想围绕 exe 创建一个包装 dll 以使用一些功能。

问题是如何从包装器 dll 访问 exe 中的 app.config 属性?

也许我的问题应该多一点,我的 exe 有以下 app.config 内容:

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

问题是如何从包装器 dll 中取出“myValue”?


感谢您的解决方案。

实际上我最初的想法是避免使用 XML 文件读取方法或 LINQ 或其他方法。我首选的解决方案是使用configuration manager libraries and the like

我将不胜感激使用通常与访问 app.config 属性相关联的类的任何帮助。

【问题讨论】:

    标签: c# .net configuration-files appsettings system.configuration


    【解决方案1】:

    ConfigurationManager.OpenMappedExeConfiguration Method 将允许您执行此操作。

    来自 MSDN 页面的示例:

    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);
    }
    

    编辑:这应该输出“myKey”值:

    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);
    

    【讨论】:

    • 您好 Espo,您的建议很棒。但也许我正在寻找的是有点不同。我已将我的问题编辑得更明确。
    • 我现在添加了一个示例,说明如何输出所需的键/值。
    【解决方案2】:

    经过一些测试,我找到了一种方法。

    1. 将 App.Config 文件添加到测试项目。使用“添加为链接”选项。
    2. 使用System.Configuration.ConfigurationManager.AppSettings["myKey"] 访问该值。

    【讨论】:

    • +1 如果您还没有发布此答案,将会发布。对于其他一些关于配置文件在一个解决方案中分布在不同项目中的场景,这也是一个很好的解决方案。
    • 这里是add as link的方法
    【解决方案3】:

    我认为您正在寻找的是:

    System.Configuration.ConfigurationManager.OpenExeConfiguration(string path)
    

    【讨论】:

      【解决方案4】:

      我赞同 Gishu 的观点,即还有另一种方法。将EXE的公共/“公共”部分抽象到DLL中创建一个包装EXE来运行它不是更好吗?这当然是更常见的发展模式。只有您希望使用的东西会进入 DLL,而 EXE 会做它当前所做的所有事情,减去进入 DLL 的东西。

      【讨论】:

      • 嗨 alastairs,我知道你有一个观点。但是我正在创建包装器 dll 作为测试程序集来测试我的 exe 中的逻辑。所以我别无选择:)
      【解决方案5】:

      它是一个xml文件,你可以使用基于Linq-XML或DOM的方法来解析出相关信息。
      (也就是说,如果没有更好的设计,我会质疑它是什么......你正在努力实现。)

      【讨论】:

        【解决方案6】:

        在 IDE 中添加链接只会在开发过程中有所帮助。我认为 lomaxx 的想法是对的:System.Configuration.ConfigurationManager.OpenExeConfiguration.

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-11-15
          • 2011-11-30
          • 1970-01-01
          • 2015-09-30
          • 1970-01-01
          • 2015-04-20
          • 1970-01-01
          • 2010-09-12
          相关资源
          最近更新 更多