如何让 ConfigurationManager 从多个文件加载应用程序设置?

发布于 2024-07-29 15:51:07 字数 1057 浏览 6 评论 0原文

我正在编写与第三方应用程序互操作的应用程序。 该应用程序通过 DLL 中的方法向开发人员公开 API。 不久前,该应用程序的供应商开始将他们自己的 .NET 组件集成到他们的程序中,当他们这样做时,他们决定他们的组件应该使用 ConfigurationManager 在运行时获取设置。

这意味着:他们的程序 foo.exe 调用 fooengine.dll,并从 foo.exe.config 读取其设置。 我的程序 bar.exe 也调用 fooengine.dll,并从 bar.exe.config 读取其设置。

嗯,这完全是错误的。 但我该如何解决它呢?

简单的解决方法是在 bar.exe.config 中复制 foo.exe.config 的设置。 这会起作用,但它很愚蠢。 这意味着从管理的角度来看,必须在 N 个不同的文件中维护给定的设置。 那迟早会失败。

我尝试将 configSource 属性放在配置文件的 appSettings 部分。 (碰巧,我使用 applicationSettings 部分进行我的设置,而他们使用 appSettings 部分进行他们的设置,所以我可以接受简单地获取该部分来自不同的文件。)但是 ConfigurationManager 不喜欢这样:它希望 configSource 中的路径不仅相对于,而且低于我的程序的目录。

我可以将它们的设置文件读取到 XmlDocument 中,然后自己进行设置。 但现在我将我的代码与其实现紧密耦合; 如果他们发布一个新版本,将设置移动到 applicationSettings 部分(现在是 2009 年,他们应该在这个位置),我的代码就会崩溃。

还有其他办法吗?

I'm writing applications that interoperate with a third-party application. This application exposes an API to developers via methods in a DLL. Some time ago, the vendor of this app started integrating their own .NET components into their program, and when they did, they decided that their components should use the ConfigurationManager to get settings at runtime.

What this means: their program, foo.exe, calls fooengine.dll, and it reads its settings from foo.exe.config. My program, bar.exe, also calls fooengine.dll, and it reads its settings from bar.exe.config.

Well, that's just plain wrong. But how do I fix it?

The simple workaround is to replicate foo.exe.config's settings in bar.exe.config. That'll work, but it's stupid. It means that from an administrative standpoint, a given setting has to be maintained in N different files. That's going to fail sooner or later.

I tried putting a configSource attribute on the appSettings section in my config file. (As it happens, I'm using the applicationSettings section for my settings, and they're using the appSettings section for theirs, so I can live with simply getting that section from a different file.) But the ConfigurationManager doesn't like that: it wants the path in configSource to be not only relative to but below my program's directory.

I can physically read their settings file into an XmlDocument and then set them myself. But now I'm tightly coupling my code to their implementation; if they put out a new release that moves the settings to the applicationSettings section (which is where they should be now that it's 2009), my code will break.

Is there another way out of this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

随遇而安 2024-08-05 15:51:07

好吧,我想我已经找到了答案,至少对于我这个问题的特定版本来说是这样。

.NET 2.0 ConfigurationManager 支持 appSettings 元素上的 file 属性。 这使您可以从外部文件获取该元素的内容。 所以我要做的是:

  1. foo.exe.config 中剪切 appSettings 元素,并将其粘贴到该目录中的另一个文件中,例如,appSettings.xml

  2. foo.exe.config 添加新元素:

  3. bar.exe.config 添加一个元素:

这有效。 但它之所以有效,是因为我的程序根本不使用 appSettings

Okay, I think I've found the answer, at least for my specific version of this problem.

The .NET 2.0 ConfigurationManager supports a file attribute on the appSettings element. This lets you get the contents of that element from an external file. So what I do is:

  1. Cut the appSettings element out of foo.exe.config and paste it into another file in that directory called, let's say, appSettings.xml.

  2. Add a new element to foo.exe.config: <appSettings file="appSettings.xml"/>.

  3. Add an element to bar.exe.config: <appSettings file="c:\program files\foo\appSettings.xml"/>

This works. But it only works because my program doesn't use appSettings at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文