当exe用作dll时是否可以访问app.config文件?
我有一个命令行应用程序,它使用常用的 Properties.Settings.blabla
属性从 app.config 文件中读取一些设置。
该应用程序也用作 dll。
当我将它用作 dll 时,它会忽略我放入目录中的任何 foo.exe.config。当使用访问配置文件的方法时,它不会像我预期的那样抛出异常,但它也不会获取配置文件。
有没有办法让用作 dll 的 exe 使用自己的设置文件,并且在使用 exe 时仍然使用 app.config 文件?我不想更改消费者代码,并且希望对 exe 代码的影响尽可能小。
我见过使用 ConfigurationManager.OpenMappedExeConfiguration
方法或 OpenExeConfiguration
的解决方案,该解决方案是否在 exe 代码中使用这些方法?这样它总是会查找配置文件,无论它是用作 dll 还是 exe?
I have a command line application that reads some settings from the app.config file, using the usual Properties.Settings.blabla
properties.
This application is also used as a dll.
When I use it as a dll, it ignores any foo.exe.config that I drop in the directory. It doesn't throw an exception as I expected when using the methods that access the config file, but it doesn't take the config file either.
Is there a way to have an exe that is being used as a dll consume its own settings file, and still have it use the app.config file when used an an exe? I don't want to change neither the consumer code, and I want as little impact as possible on the exe code.
I've seen solutions using ConfigurationManager.OpenMappedExeConfiguration
Method or OpenExeConfiguration
, is the solution using those methods inside the exe code? That way it will always look for the config file, regardless of if it is being used as dll or exe?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想这样做,则需要专门从该文件中读取。 .NET 可执行文件从它自己的
app.config
文件中读取。库代码总是从正在执行的应用程序的app.config
文件中读取...甚至库代码恰好被编译成具有.exe扩展名的文件。编辑以强调/澄清
当我谈到
app.config
时,我当然是在谈论适当重命名的[program.exe.config]
文件。If you want to do that, you'll need to read specifically from that file. A .NET executable reads from it's own
app.config
file. Library code always reads from theapp.config
file for the application being executed... even library code that just happens to have been compiled into a file with an .exe extension.Edit for Emphasis/Clarification
When I speak of an
app.config
, I am of course talking about the appropriately-renamed[program.exe.config]
file.