C# 获取另一个exe的App.Config
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/53545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the App.Config of another Exe
提问by Graviton
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.
我有一个带有App.Config
文件的exe 。现在我想围绕 exe 创建一个包装 dll 以使用一些功能。
The question is how can I access the app.config property in the exe from the wrapper dll?
问题是如何从包装器 dll 访问 exe 中的 app.config 属性?
Maybe I should be a little bit more in my questions, I have the following app.config content with the exe:
也许我应该在我的问题中多一点,我的 exe 有以下 app.config 内容:
<?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?
问题是如何从包装器 dll 中获取“myValue”?
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.
实际上,我最初的概念是避免使用 XML 文件读取方法或 LINQ 或其他方法。我的首选解决方案是使用配置管理器库等。
I'll appreciate any help that uses the classes that are normally associated with accessing app.config properties.
对于使用通常与访问 app.config 属性相关联的类的任何帮助,我将不胜感激。
采纳答案by Graviton
After some testing, I found a way to do this.
经过一些测试,我找到了一种方法来做到这一点。
- Add the App.Config file to the test project. Use "Add as a link" option.
- Use
System.Configuration.ConfigurationManager.AppSettings["myKey"]
to access the value.
- 将 App.Config 文件添加到测试项目。使用“添加为链接”选项。
- 使用
System.Configuration.ConfigurationManager.AppSettings["myKey"]
访问值。
回答by Gishu
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.)
它是一个 xml 文件,您可以使用 Linq-XML 或基于 DOM 的方法来解析出相关信息。
(也就是说,我会质疑是否没有更好的设计来满足您正在努力实现的目标。)
回答by Espo
The ConfigurationManager.OpenMappedExeConfiguration Methodwill allow you to do this.
该ConfigurationManager.OpenMappedExeConfiguration方法可以让你做到这一点。
Sample from the MSDN page:
来自 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);
}
EDIT: This should output the "myKey" value:
编辑:这应该输出“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);
回答by alastairs
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.
我同意 Gishu 的观点,那就是还有另一种方式。将 EXE 的公共/“公共”部分抽象到 DLL 中创建一个包装器 EXE 来运行它不是更好吗?这当然是更常见的发展模式。只有您希望使用的内容才会进入 DLL,而 EXE 将执行它当前所做的所有内容,减去进入 DLL 的内容。
回答by lomaxx
I think what you're looking for is:
我想你要找的是:
System.Configuration.ConfigurationManager.OpenExeConfiguration(string path)
回答by tkrehbiel
Adding a link in the IDE would only help during development. I think lomaxx has the right idea: System.Configuration.ConfigurationManager.OpenExeConfiguration.
在 IDE 中添加链接只会在开发过程中有所帮助。我认为 lomaxx 有正确的想法:System.Configuration.ConfigurationManager.OpenExeConfiguration.